![]() |
|
Pointers to variables inside a struct |
aeger
Member #5,769
April 2005
|
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? |
CGamesPlay
Member #2,559
July 2002
![]() |
Take off all of the &, and it should work. [append] [append] -- Ryan Patterson - <http://cgamesplay.com/> |
Synapse Jumps
Member #3,073
December 2002
|
What type is vsp_map? Is it a pointer? |
CGamesPlay
Member #2,559
July 2002
![]() |
He didn't say he wasn't getting values for things like name_len, w, and h. He just said "does not work" -- Ryan Patterson - <http://cgamesplay.com/> |
aeger
Member #5,769
April 2005
|
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;
|
CGamesPlay
Member #2,559
July 2002
![]() |
You forgot to allocate memory for the variables -- Ryan Patterson - <http://cgamesplay.com/> |
Synapse Jumps
Member #3,073
December 2002
|
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: 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. |
aeger
Member #5,769
April 2005
|
-_- 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 |
|