二进制文件 - obj(游戏)保存并加载obj

时间:2016-09-04 16:06:06

标签: c++

此副本此地址(我不想复制地址)然后我发布第一个对象All Course 这个练习课 我必须意识到保存并加载游戏Treasure Hunt

我的档案: 主要:

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();
}

和&#39;:

#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

这是要复制的Func:

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

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

球员:

#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
陷阱:

#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

和宝藏:

#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

并且borad来自这个类:

#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

这是要发挥的功能:

    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);

}

1 个答案:

答案 0 :(得分:3)

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

如果班级的所有数据成员都是POD types并且没有资源,您可能只会这样做,即使这样,也会出现structure padding的问题

免费建议,不要明确表达,即使对于非资源拥有的POD类型,它通常也不可移植。

注意:指针拥有或引用资源(内存)。

不同的编译器可能pad your structure并以不同的方式将其放在内存中。

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

除了我之前所说的,这也是一个非常糟糕的主意。您处于this的上下文中,并在外部修改整个this,而它还活着......

要解决您的问题,请阅读关于Serialization in C++