Archived

This topic is now archived and is closed to further replies.

Jesper T

Saving structs to file question

Recommended Posts

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 ?
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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 anywhere
extern 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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Do note that in order to write data to a struct as one into a file you must write it as binary and read it the same way too. Similar to how the first reply is. But he forgot to mention that the file has to be opened as binary using something like

ifstream inFile(filename, ios::binary);

Using the FILE * I think the syntax is:

FILE *stream = fopen(filename, "#b")

The pound symbol being anything other paramters. The b means binary. ''r'' is read binary, and ''w'' is write binary. There are other parameters that you can use here but it has to be binary.

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Share this post


Link to post
Share on other sites