Tuesday, July 28, 2009

How 2 write structure in a file wid VC++?

i want 2 write a structure in a file with this format


struct a{


char b;


int c, d[12][15];


}








when i use fwrite function its not working....


may be bcoz i dont know the proper format of function....








plz help me ....tell me the function to write wid its usage in the program....

How 2 write structure in a file wid VC++?
Hi,


struct a{


char b;


int c, d[12][15];


}; (IT SHOULD BE TERMINATED BY A ;)





struct a aa; //declare structure variable


FILE *fp //file pointer


fwrite(%26amp;aa,sizeof(aa),1,fp);


fread(%26amp;aa,sizeof(aa),1,fp);


it will write/read all the elements of a structure at a time.
Reply:Fwrite has no way to know how to write a struct. To write a struct, or an object instance, to a file, you have to write each element of the struct (or object) individually. You also have to provide a way to read it back in in the same order.





Hope that helps.
Reply:You can't write the structure directly to file, you need to serialize the data in it first. Search for "Structure serialization" in any search engine and you'll get lots of info about it.


No comments:

Post a Comment