I have no idea how to use pack_fread() with variables inside a struct.
The code below illustrates one of my tries on how to do that, however it does not work :/
void vsp_map_load(char *filename) { PACKFILE *rFile; rFile = pack_fopen(filename, "r"); int name_len, tileset_len; pack_fread(&name_len, sizeof(int), rFile); pack_fread(&(vsp_map.name), name_len * sizeof(char), rFile); pack_fread(&tileset_len, sizeof(int), rFile); pack_fread(&(vsp_map.tileset), tileset_len * sizeof(char), rFile); pack_fread(&(vsp_map.w), sizeof(int), rFile); pack_fread(&(vsp_map.h), sizeof(int), rFile); pack_fread(&(vsp_map.tile), (vsp_map.w * vsp_map.h) * sizeof(TILE), rFile); pack_fclose(rFile); }
Could somebody please tell me the right way of doing this?
]]>Take off all of the &, and it should work.
[append]
Hmm, on second thought... maybe that isn't the problem...
[append]
Yes, it is. Take the &s off of the variables which are arrays or pointers, and it will work. Leave them on the others.
What type is vsp_map? Is it a pointer?
EDIT:
CGames: if that were the case, he should DEFINATELY be getting values for things like w and h
He didn't say he wasn't getting values for things like name_len, w, and h. He just said "does not work"
]]>It still errors ;_;
void vsp_map_load(char *filename) { PACKFILE *rFile; rFile = pack_fopen(filename, "r"); int name_len, tileset_len; pack_fread(&name_len, sizeof(int), rFile); pack_fread(vsp_map.name, name_len * sizeof(char), rFile); pack_fread(&tileset_len, sizeof(int), rFile); pack_fread(vsp_map.tileset, tileset_len * sizeof(char), rFile); pack_fread(&(vsp_map.w), sizeof(int), rFile); pack_fread(&(vsp_map.h), sizeof(int), rFile); pack_fread(vsp_map.tile, (vsp_map.w * vsp_map.h) * sizeof(TILE), rFile); pack_fclose(rFile); }
When I only read the first int it works alright but when I try to read the string it crashes.
EDIT:
This is vsp_map:
typedef struct MAP { char *name; char *tileset; int w; int h; struct TILE * tile; }MAP; MAP vsp_map;
]]>
You forgot to allocate memory for the variables
]]>Heh. You need to read up on Pointers, my young friend. For things like name and tileset and tile you need to do things first:
FIRST you need to find out how long the saved "name" is, then you need to allocate memory for it, then you can read/copy it. Here's an example:
EDIT: Oops, didn't see you read in the length of the name, new code:
vsp_map.name = new char [ name_len + 1 ]; //+1 to allow for null-character pack_fread(vsp_map.name, name_len * sizeof(char), rFile);
You need to do equivalent stuff for your other pointers.
Your other data SHOULD be fine. Check everything that ISN'T a pointer and see if you're getting correct data.
]]>-_-
I can't believe it was that simple. Sad thing is, this is the second time this happened to me this week.
Oh well, hopefully I won't do it again
Thanks guys
]]>