Saving structs to file question

Started by
10 comments, last by Jesper T 22 years, 5 months ago
Im using (ifstream and ofstream) to load and save files.. In my prog I have som rather complicated structs and I was wondering if there is any way to save/load all the data of a structure to/from a file without accessing all the data members of it, I''ve tried to just do like this: saveTo << someStruct; ...but it didnt work.. Has anyone got any tips about this ?
Advertisement
If your struct contains non-pointer members then you should be able to write the whole struct to a file like so:

MyStruct mystruct;
fstream myfile;

myfile.write((const char*)&mystruct, sizeof(mystruct));

Using operator << will not work unless you define it for your struct.




Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
If you do it that way, just make sure you load it with the same program or programs developed with the same compiler on the same architecture. (because of byte padding)
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
Thanks a lot for your help , but im afraid it contins pointers both to standard double/int values and to other structs.. :/ Is there any other methods that u know of ? ..Or will I just have do it the hard way and save the members one by one ?


Well, you''ll have to save the members one by one, BUT:

You can overload the << operator for streams in such a way that later, you CAN say

oFile << myStruct;

or whatever. The operator needs to be declared a friend to your class, though, if you have any private members that you want accessed. You''ll also need to overload the >> operator if you want to be able to read the structs back in again.

--Riley
ok, thanks a lot for the replys


Yes, there is. It's called operator overloading. You can't just ue the << since you have pointers and storing the memory address isn't what you want. Though I'm not sure if it works with structs.

In your class (struct):
      class zbTileType{public:friend std::ostream &operator<<(std::ostream &stream, zbTileType obj);friend std::istream &operator>>(std::istream &stream, zbTileType &obj);std::string Name;std::string FileName;int NumFrames.};  



In your implementation:

  std::ostream &operator<<(std::ostream &stream, zbTileType obj){	stream << obj.Name << std::endl;	stream << obj.NumFrames << std::endl;	stream << obj.FileName << std::endl;	return stream;}std::istream &operator>>(std::istream &stream, zbTileType &obj){	stream >> obj.Name;	stream >> obj.NumFrames;	stream >> obj.FileName;		return stream;	}      


This is to overload the << and >> operators so you can open a file and just use them as if they like cout and cin.

Edited by - Darkor on October 30, 2001 10:01:57 PM
hmmm ok, ill look into it.. thanks again for the help ppl.


I use this since years for my CFG file and this work fine
I think you must avoid CString value or so (variable lenght)

typedef struct{	char LastPresetUsed[MAX_PATH];        int screen;        BOOL whatewer;        //and so on}Globale_CFG;//I love to acces my CFG value from anywhereextern Globale_CFG CFG;////////////////////////////////// LOAD CFG FILE // or create one if first launch////////////////////////////////void LoadCFG(){   FILE *	stream;   CDDir(BIN);   //open or create the file if doesn't exist   stream  = fopen( "JoyGame.cfg", "rb" );   if(stream==NULL)   {      //file not present create it      stream  = fopen( "JoyGame.cfg", "wb" );      if(stream==NULL)      {	AfxMessageBox("Unable to create cfg file\n",MB_OK);      }      else      {         // this init all the value to a default value        // this is usually the first launch of the proggy	DefaultCFG();        // write the structure	fwrite( &CFG, sizeof( CFG ), 1, stream );	fclose( stream );       }   }   else   {	//read the CFG file	fread( &CFG, sizeof( CFG ), 1, stream );	fclose( stream );    }}////////////////////////////////// SAVE CFG FILE // ////////////////////////////////void SaveCFG(){    FILE *	stream;    CDDir(BIN);        //file not present create it    stream  = fopen( "JoyGame.cfg", "wb" );    if(stream==NULL)    {	AfxMessageBox("SaveCFG->Unable to write to cfg file\n",MB_OK);    }    else    {	fwrite( &CFG, sizeof( CFG ), 1, stream );	fclose( stream );     }} 



Dan

Edited by - dansteph on November 1, 2001 4:50:54 PM
If your structs contain pointers to other portions of the same file you can put file offset values, by using the handy ftell routine, into the pointer locations instead of actaul addresses. Then at load time you can run through your struct and fixup all the pointers by just adding the address of the head of the struct right into the pointer offsets.

This topic is closed to new replies.

Advertisement