Archived

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

Arbz

binary data files?

Recommended Posts

Arbz    122
can i save game data (such as missions, maps and music/sounds) into binary files somehow, or some other way that doesnt let the user just open it up in notepad and change attributes? The only non-commercial games that use .dat files and text files to save high scores, and misison data allow the user to open them up and edit them. Is there a way out of this, such as how games (RA2 for example), save the maps in specialised file. I would like to find out, and if anyone could tell me how to do it or give me a pointer.# cheers Arbz

Share this post


Link to post
Share on other sites
invective    118
In the simplest form you can just dump your data to a binary file:

std::ofstream out;
out.open ("output.dat", std::ios::binary);
out.write (reinterpret_cast(&MyStructure), sizeof (MyStructure));
out.close();


Do exactly the opposite with read to get it back. Note that you do not want to save pointers, and this method will save any padded bytes in your structure too, so you might want to write out elements of the structure individually. If you have many structures to save, overload a << operator and >> operator for each one, and your saving and loading code will be much simpler and easier to read, not to mention easier to update if you change the data fields later on.

If you are really paranoide, you can encrypt your data on store / load too (i really don''t think its worth it). Also consider using zlib (www.zlib.org) if you want to reduce your file size.

Share this post


Link to post
Share on other sites
Arbz    122
thanks for the reply.

when you say do the exact opposite to read, do you mean:

std::ifstream in;
in.open ("output.dat", std::ios::binary);
in.write (reinterpret_cast(&MyStructure), sizeof (MyStructure));
in.close();


also, would this save the contents of "MYStructure" to the file? im sorta new to this filestream stuff.

it is in the fstream header right?


thanks
arbz



[edited by - arbz on March 18, 2002 4:05:40 PM]

Share this post


Link to post
Share on other sites
invective    118
Yes, its in the fstream header, but you have to change read for write, and the board seems to have eaten the char* in my reinterpret_cast -- guess I need a source tag ( I really hate the new tiny source box -- why is it so tiny?!?!!?).


      
#include <fstream>

MyStruct m;
...
std::ifstream in;
in.open ("output.dat", std::ios::binary);
in.read (reinterpret_cast<char*>(&m), sizeof (MyStruct));
in.close();

}


again this only works if MyStruct has no pointers in it because it will save the address the pointer points to (garbage) and not the data pointed to, since logically only the pointer (not the stuff pointed to) is actually part of the structure. If you have pointers you need to dereferce and save them yourself.

see:
http://cplusplus.com/ref/iostream/fstream/

[edited by - invective on March 18, 2002 4:30:51 PM]

Share this post


Link to post
Share on other sites