Original Post
I''ve been reading your book "Teach Yourself C++ in 21 Days 3rd ED.", it is a great book! But still I have a problem
Ok, I''m trying to create a class that could save itself, that is, save all it''s member datas to a file.
Unfortunately I''m having problems...Have a look, if you dare:
class COORDIM
{
private:
int coorX, coorY;
int dimWidth, dimHeight;
public:
//accessors
int GetX() const { return (coorX); }//end inclince
int GetY() const { return (coorY); }//end incline
int GetWidth() const { return (dimWidth); }//end inclince
int GetHeight() const { return (dimHeight); }//end incline
void SetX(int x) { coorX = x; }//end incline
void SetY(int y) { coorY = y; }//end incline
void SetWidth(int width) { dimWidth = width; }//end incline
void SetHeight(int height) { dimHeight = height; }//end incline
};
class TILEMAP
{
private:
LPCTSTR MapFile; //The filename to be saved or loaded
COORDIM MapInfo; //Contains the coordinates and dimension
public:
//Constructors
TILEMAP(LPCTSTR FileName); //Constructor, initial filename
~TILEMAP(); //Deconstructor
//public accessors
LPCTSTR GetMapFile() const //Used to get the filename
{
return MapFile;
}//end incline
void SetMapFile(LPCTSTR FileName) //Set MapFile
{
MapFile = FileName;
}//end incline
COORDIM GetMapInfo() const
{
return (MapInfo);
}//end incline
void SetMapInfoX(int x) { MapInfo.SetX(x); }
void SetMapInfoY(int y) { MapInfo.SetY(y); }
void SetMapInfoWidth(int width) { MapInfo.SetWidth(width); }
void SetMapInfoHeight(int height) {MapInfo.SetHeight(height); }
int SaveMap(); //Need to include fstream.h to use
int LoadMap(); //Needs fstream.h
};
///Constructor
TILEMAP::TILEMAP(LPCTSTR Filename):
MapFile(Filename) //Initialize MapFile
{
}///End Constructor
///Deconstructor
TILEMAP::~TILEMAP()
{
}///End Deconstructor
/////LoadMap
TILEMAP::SaveMap()
{
ofstream CurrentFile(MapFile, ios::binary);//Open file for binary
if (!CurrentFile)
{
cout << "Error opening file " << MapFile << "for writing.\n";
return (0);
}//end if
CurrentFile.write((char *)&MapInfo, sizeof(MapInfo));
//success
return (1);
}
Ok, the fourth line from the bottom...
CurrentFile.write((char *)&MapInfo, sizeof(MapInfo));
doesn''t make any sense to me although I came up with it. Basically I want to write the data stored to MapInfo to the file. How do I know which block of memory on the file it is writen to in case I have more than one class data that I want to save and later load? Or is there a better way to save informations to a file? For example, if you''re making a game which has a Level.lev file and that should contains data of the classes LevelName, LevelCharaters, etc...Is there a way to write/read certain bits of memory from a file in whole classes?