fwrite - pointer array of struct issues.

Started by
6 comments, last by Xero-X2 21 years, 1 month ago
This does not work.
  
int SIZEOFARRAY=100;
char *FileName; 
CreatureLOAD *contatiner;
contatiner=new CreatureLOAD[SIZEOFARRAY];

... <- FileName is set in here as well as the data is set.


FILE *dat=NULL;
dat = fopen(FileName,"wb");

BYTE temp=SIZEOFARRAY;
fwrite(&temp,sizeof(BYTE),1,dat);

fwrite(contatiner,sizeof(CreatureLOAD)*(SIZEOFARRAY),1,dat);
fclose(dat);
  
This does work.
  
int SIZEOFARRAY=100;
char *FileName; 
CreatureLOAD contatiner[100];

... <- FileName is set in here as well as the data is set.



FILE *dat=NULL;
dat = fopen(FileName,"wb");

BYTE temp=SIZEOFARRAY;
fwrite(&temp,sizeof(BYTE),1,dat);

fwrite(contatiner,sizeof(CreatureLOAD)*(SIZEOFARRAY),1,dat);
fclose(dat);
  
I can see that that means that the problem is useing the pointer to the stucture how can I fix this while still maintaining the dynamic array size? thanks.
"I seek knowledge and to help those who also seek it"
Advertisement
I can''t spot the problem. Your pointers are fine, because the array name behaves (note careful word choice) like a pointer in that context.

Must be something else. Is the file empty after you do the write and close it?


[edited by - kdogg on March 5, 2003 6:50:49 PM]
You can use malloc. Replace:

contatiner=new CreatureLOAD[SIZEOFARRAY];

with:

contatiner = (CreatureLOAD*)malloc(SIZEOFARRAY*
sizeof(CreatureLOAD));
The file contains other portions of memory if I use it under the one that dosn''t work, it saves Text boxes from the program instead of the varibles that are in the struct.
"I seek knowledge and to help those who also seek it"
contatiner = (CreatureLOAD*)malloc(SIZEOFARRAY*sizeof(CreatureLOAD));

did not effect anything.
"I seek knowledge and to help those who also seek it"
Put the malloc back to new, that''s not the problem.

I don''t see how this could cause the error, but you are using fwrite in an unintuitive way. The 2nd argument should be the size of one structure to be written and the 3rd argument should be the number of those structures to write. So it makes more sense to write:


  fwrite(container, sizeof(CreatureLOAD), SIZEOFARRAY, dat);  


As Zipster said I''m not sure why the two examples don''t both work. Did you post the exact code you''re using? You aren''t using sizeof(container) anywhere are you?
It is similar to the exact I will post the exact code but it should be equal to the modifided code.

I didn't think so, because new uses malloc anyway.

It saves memory that is in a textbox instead of the data stored in the varibles.

Yes it is true my use of fwrite was a bit odd I forgot to return it to its right full form after I was messing with it. It just multiplies those two values together any way if my experences are correct so it shouldn't technicnically matter, but I have returned it to its proper form.


    ...{if (SaveDialog1->Execute()){CreatureLOAD *contatiner;contatiner=new CreatureLOAD[Memo2->Lines->Count/10];for (int x=0; x< Memo2->Lines->Count/10; x++){strcpy(contatiner[x].name,Memo2->Lines->Strings[x*10].c_str());strcpy(contatiner[x].desc,Memo2->Lines->Strings[(x*10)+1].c_str());contatiner[x].lvl=StrToInt(Memo2->Lines->Strings[(x*10)+2].c_str());contatiner[x].HP=StrToInt(Memo2->Lines->Strings[(x*10)+3].c_str());contatiner[x].ARTYPE=StrToInt(Memo2->Lines->Strings[(x*10)+4].c_str());contatiner[x].defencepower=StrToInt(Memo2->Lines->Strings[(x*10)+5].c_str());contatiner[x].DMGTYPE=StrToInt(Memo2->Lines->Strings[(x*10)+6].c_str());contatiner[x].attackpower=StrToInt(Memo2->Lines->Strings[(x*10)+7].c_str());contatiner[x].CARDNUMBER=StrToInt(Memo2->Lines->Strings[(x*10)+8].c_str());// line 9 is pointless, don't even look at it.}FILE *dat=NULL;dat = fopen(SaveDialog1->FileName.c_str(),"wb");BYTE temp=Memo2->Lines->Count/10;fwrite(&temp,sizeof(BYTE),1,dat);fwrite(contatiner,sizeof(CreatureLOAD),(Memo2->Lines->Count/10),dat);fclose(dat);}}...    


[edited by - Xero-X2 on March 6, 2003 4:14:41 PM]
"I seek knowledge and to help those who also seek it"

This topic is closed to new replies.

Advertisement