Archived

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

ReaperX

What functions for saving and loading?

Recommended Posts

ReaperX    122
What C++ functions should I use to save my map data? I want to isolate all my save/load operations in a file manager object so I can switch from multiple files to one big file later with out the rest of the engine knowing. I have read an article that recommended write() and read() but then i read in another forum that those functions were not safe to use. Anyone have a better suggestion? How should i structure my file mananager? Clarification: What i have been doing is taking the structures (struct MapData, struct Tile, etc...) and using "out.write( (char*) &TheTile, sizeof(TheTile))" to put them into a file. I have however heard that variables can be of different sizes on different systems (I am not certain what is meant by this). Will this cause difficulty later? Is there a better or easier way to save this map data? What if later I wanted to stick all my maps and other data into a single file? I wanted to make FileManager class accessable through functions that acted like write and read and so that the manager would slap a header on the chunk of data and put it into a single file without the other objects knowing. Would this work? Am I forgetting anything that might cause problems later? [edited by - ReaperX on May 27, 2003 9:49:49 AM] [edited by - ReaperX on May 27, 2003 9:50:36 AM]

Share this post


Link to post
Share on other sites
capn_midnight    1707
for C++ you should use the ofstream and ifstream for saving and loading

ofstream ofs;
ofs.open("File.txt");
ofs<<"Writing to the file!"<ofs.close();

ifstream ifs;
ifs.open("File.txt");
int n;
ifs>>n;
cout<<"Read in "<ifs.close;

there you go!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
If you are using a binary data format for your map, you should use the read and write functions of the std::ifstream and std::ofstream, like this...

#include <fstream>

char Buffer[100];
std::ifstream In( "<SOMEFILE>", std::ios::binary );
In.read( Buffer, 99 );

//and to write the buffer ...
std::ofstream Out( "<SOMEFILE>", std::ios::binary );
Out.write( Buffer, 99 );

Of course, you can use the C library functions fopen, fread, and fwrite as well

Share this post


Link to post
Share on other sites