Jump to content
  • Advertisement

Archived

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

Jesper T

Saving structs to file question

This topic is 6106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!