Dudu Moyal Dudu Moyal - 3 months ago 8
C++ Question

Binary file- obj(game) save and load the obj

this copy this address(i dont whant to copy address )and then I release the first object All Course
This exercise class
I have to realize saving and loading the game Treasure Hunt

My files :
Main :

srand(time(0));
TreasureSeeker dudu;
int save = 0;
cout << "To New game prees 1 to load game prees 2 : ";
cin >> save;
while (save !=1 &&save !=2 )
{
cout << "To New game prees 1 to load game prees 2 : ";
cin >> save;
}
if (save == 1)
{
ifstream inFile("try.dat", ios::binary);
TreasureSeeker temp(inFile);
inFile.close();
temp.play();
}
else
{
dudu.setname();
dudu.play();
ofstream outFile("try.dat", ios::binary | ios::trunc);
dudu.save(outFile);
outFile.close();
}


and':

#define _CRT_SECURE_NO_WARNINGS
#ifndef _TreasureSeeker_H
#define _TreasureSeeker_H
#include "GamePiece.h"
#include "Player.h"
#include "Treasure.h"
#include"Enemy.h"
#include"Trap.h"

using namespace std;

//class Treasusekeer

class TreasureSeeker
{
public:
TreasureSeeker();
~TreasureSeeker();
void play();
void save(ofstream & out)const;
TreasureSeeker(ifstream& in);
void setname();
private:
Player *p1;//obj to player
Treasure* t1;//obj to Treasure
Trap *trap1;//obj to Trap
Enemy *en1;//obj to Enemy
GamePiece borad[9][9];
GamePiece *emp1;//obj empty
GamePiece temp;//obj temp to save for a enemy move
void check(int flag);

void print();
void del();
void check(int i, int j, int move);
void step();
void checkenemy(int p);
};

#endif


This is the Func to copy :

void TreasureSeeker::save(ofstream & out)const
{
out.write((const char*)this, sizeof(*this));
}

TreasureSeeker::TreasureSeeker(ifstream& in)
{
in.read((char*)this, sizeof(*this));
}


player :

#include "GamePiece.h"
#ifndef _Player_H
#define _Player_H

//class player

class Player : public GamePiece
{

public:

Player();
~Player();
void print();
int Neutralization();
int Step();
int getX(){ return this->placeX; }
int getY(){ return this->placeY; }
void setx(int x){this->placeX = x;}
void sety(int y){this->placeY = y;}
void fight();
void findknife();
void setn(string d){ this->nameplayer = d; }

private:
string nameplayer;
int knife;
int placeX;
int placeY;
int killenemy;
int killtrap;

};
#endif


trap :

#include"GamePiece.h"
#ifndef _Trap_H
#define _Trap_H

class Trap :public GamePiece
{
public:
Trap();
~Trap();
void puttrap(int x,int y);
int getnumoftrap();
private:
int numoftrap;
};

#endif


and Treasure :

#include"GamePiece.h"
#ifndef _Treasure_H
#define _Treasure_H
class Treasure :public GamePiece
{
public:
Treasure(int x, int y);
~Treasure();
int getX();
int getY();
int Gettrusefake();

private:
int TreasureX;
int TreasureY;
int Treasurefake;
};

#endif


and the borad is from this class :

#define _CRT_SECURE_NO_WARNINGS
#ifndef _GamePiece_H
#define _GamePiece_H
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <ctype.h>
#include <time.h>
#include <typeinfo>
#include <stdlib.h>
using namespace std;

//class gamepiece +func get set print

class GamePiece
{
public:
GamePiece(){ nametool = "Empty Cell"; charname = 'O'; }
~GamePiece(){};
void printname(){ cout << "name tool is :" << nametool << endl; }
void printchar(){ cout << charname; }
virtual void step(){};
char getname(){ return charname; }
string getoolname(){ return nametool; }
void setcharname(char p){ charname = p; }



private:
string nametool;
char charname;
};

#endif


and this is the func to play :

cout << "Hello to Find the Treasure game" << endl;
int save = 1;

do
{
cout << endl;
p1->print();
cout << endl;
print();
step();
cout << endl;
print();
cout << "to save end exit prees 0 to continue prees 1 :" << endl << " your choise : ";
cin >> save;
} while (save);

}

Answer
void TreasureSeeker::save(ofstream & out)const
{
    out.write((const char*)this, sizeof(*this));
}

You may only do this if all the data members of the class are POD types and do not own a resource, even with that, there comes the issue of structure padding.

Free advice, don't do this expressly, its usually not portable even for non-resource owning POD types.

NOTE: A pointer either owns or refers to a resource (memory).

Different compilers may pad your structure and lay it out in memory differently.

TreasureSeeker::TreasureSeeker(ifstream& in)
{
    in.read((char*)this, sizeof(*this));
}

In addition to what I said previously, this is also a pretty bad idea. You are in the context of this, and externally modifying the whole of this, while its alive....


To solve your problem, read About Serialization in C++