Jump to content
  • Advertisement

Archived

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

programering

How to load/store classes data in filebuffers?

This topic is 5567 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

How to load/store class data in filebuffers? Can I just load/store it directly to the class? Or must I take it via a struct? Anton Karlsson Klingis Entertainment Games with silly humor

Share this post


Link to post
Share on other sites
Advertisement
//Ok You want to Store a Struct in a File and Load it ...
//See this
//Suppose

#include <fstream.h>
struct MyStruct{
int x;
float y;
char t;
};

//For Saving into File ...
MyStruct mystruct;
//Fill my Struct with data here ...
//*********************************
ofstream ofs("File.txt");
ofs.write((char*)&mystruct,sizeof(mystruct));

//For Loading
ifstream ifs("File.txt");
ifs.read((char*)&mystruct,sizeof(mystruct));
//*********************************
That is all

Come to visit Egypt (the Land of Civilization)

Share this post


Link to post
Share on other sites
First, you must use <fstream> and not <fstream.h>.
Second, you may only serialize that way classes that have no virtual functions, no pointer, reference or object members. These are known as POD (plain old data) classes.

The proper way to save an object is to define a function that explicitely save and load the relevant data members. Do not indiscriminately use bitwise copying.





[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Im actually working on this same problem now, im trying to come up with a generalish solution to it also,
http://www.gamedev.net/community/forums/topic.asp?topic_id=171088

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!