我已经使用Visual C ++ 2008 Express为基于文本的RPG游戏进行了三个文件。在我真的潜入整个事情之前,我想让基础熨烫:新游戏,节省游戏,继续游戏,退出游戏。到目前为止,我有一个字符部分(在这种情况下找到武器)并退出游戏全部熨烫。我被困在如何将武器统计传递给保存文件。我似乎没有问题的构建成员,并检查文件以查找“垃圾”:ì和-858993460代替我的值。

我应该如何解决修复我的save_game和continue_game函数?我已经做了很多研究试图弄清楚这一点,没有什么我尝试过似乎有所帮助。

这是代码的重要作品:

struct player_character
{
char type;
int damage;
int stability;
};

void save_game(player_character& pc, ofstream &save);
void continue_game(player_character& pc, ifstream &get);

int main()
{   
player_character pc;
ofstream save;
ifstream get;

//rest of main() goes here.

//pause screen
system("pause");
return 0;
}

//rest of functions go here.

void save_game(player_character &pc, ofstream &save_data) 
{
save_data.open  ("save.dat", ios::binary);
    if (save_data.is_open())
    {
    save_data << "pc.type = " << pc.type << endl;
    save_data << "pc.damage = " << pc.damage << endl;
    save_data << "pc.stability = " << pc.stability << endl;
                //doesn't work
    //save_data.write(reinterpret_cast<char*>(&pc), sizeof(pc));
    save_data.close();
    }
    else
    {
    cout << " Error. Unable to open file.";
    }
}

void continue_game(player_character &pc, ifstream &get_data) 
{
get_data.open("save.dat");
if (get_data.is_open())
    {
                //doesn't work
    //get.read(reinterpret_cast<char*>(&pc), sizeof(pc));
    get.close();
    }
    else
    {
    cout << " Error. Unable to open file.";
    }
}
.


感谢响应。我正在尝试以下修订。 Continue_game函数似乎有效。我没有错误(尚未)。当我在制作角色后选择保存时,我收到以下错误:0x69197A28中的未处理异常在undogle.exe:0xC0000005:Access违规读取位置0xFFFFFFCCCCC。

谷歌将其显示为某种Windows问题。

为什么我的函数导致此错误?

void save_game(ofstream &save, player_character const &pc) 
{
save.open  ("save.dat");
    if (save.is_open())
    {
    save.write(reinterpret_cast<char const *>(pc.type), sizeof pc.type); 
    save.write(reinterpret_cast<char const *>(pc.damage), sizeof pc.damage); 
    save.write(reinterpret_cast<char const *>(pc.stability), sizeof pc.stability); 
    save.close();
    }
    else
    {
    cout << " Error. Unable to open file.";
    }
}

int continue_game(ifstream &get) 
{
if (!get.read(reinterpret_cast<char *>(&pc.type), sizeof pc.type)) { /* error */ } 
if (!get.read(reinterpret_cast<char *>(&pc.damage), sizeof pc.damage)) { /* error */ } 
if (!get.read(reinterpret_cast<char *>(&pc.stability), sizeof pc.stability)) { /* error */ } 

return pc.type;
return pc.damage;
return pc.stability;
}
.


我已更改save_game和continue_game代码并在此包含它。我还收集了调试器和Autos输出(小版本的Autos,而不是全部七页)。显然,我的价值观未在Save_game中进行评估,因此Continue_game没有任何操作,不会导致错误。

这是代码和调试器/ autos打印输出:

int save_game(ofstream &save, player_character& pc) 
{
save.open  ("save.dat", ios::binary);
if (save.is_open())
{
//the error hits here:
    save.write(reinterpret_cast<char const *>(pc.type), sizeof pc.type); 
    save.write(reinterpret_cast<char const *>(pc.damage), sizeof pc.damage); 
    save.write(reinterpret_cast<char const *>(pc.stability), sizeof pc.stability); 
    save.close();
}
else
{
    cout << " Error. Unable to open file.";
}
return pc.type;
return pc.damage;
return pc.stability;
}

int continue_game(ifstream &get, player_character& pc)
{
get.open  ("save.dat", ios::binary);
if (get.is_open())
{
    if (!get.read(reinterpret_cast<char *>(&pc.type), sizeof pc.type)) { /* error */ } 
    if (!get.read(reinterpret_cast<char *>(&pc.damage), sizeof pc.damage)) { /* error */ } 
    if (!get.read(reinterpret_cast<char *>(&pc.stability), sizeof pc.stability)) { /* error */ } 
    get.close();
}
else
{
    cout << " Error. Unable to open file.";
}
return pc.type;
return pc.damage;
return pc.stability;
}
.

调试器输出窗口: 在0x644e7a28中的第一次机会异常在undone.exe:0xC0000005:Access违规读取位置0xFFFFFFCCCCC。 Undogle.exe 0x644e7a28的未处理异常:0xc0000005:Access违规读取位置0xFFFFFFCCCC。

autos: - PC {Type='ì'损伤= -858993460稳定性= -858993460} Player_character& 键入-52'ì'char 伤害-858993460 int. 稳定性-858993460 int. pc.type -52'ì'char - 保存{_filebuffer= {...}} std :: basic_ofstream>& + std :: basic_ostream> {...} std :: basic_ostream> + _fileBuffer {_PCVT= 0x00000000 _MYCHAR='ì'_wrotesome= false ...} std :: basic_filebuf>


好吧,我会设法进行一些进步。我发现我需要将参数放入我的main_menu函数(介意你不是关于main()函数,而是我制作的一个函数,以便他们将传递给我的save_game函数。我也能够通过添加&进入我的写入功能来停止访问错误。 这是:
save.write(reinterpret_cast<char const *>(&pc.type), sizeof pc.type); 
save.write(reinterpret_cast<char const *>(&pc.damage), sizeof pc.damage); 
save.write(reinterpret_cast<char const *>(&pc.stability), sizeof pc.stability); 
.

而不是:

save.write(reinterpret_cast<char const *>(pc.type), sizeof pc.type); 
save.write(reinterpret_cast<char const *>(pc.damage), sizeof pc.damage); 
save.write(reinterpret_cast<char const *>(pc.stability), sizeof pc.stability); 
.

在将数据放入文件中时,Savve_game代码仍然无法正常工作,但它会将其打印到屏幕。

有帮助吗?

解决方案 3

看起来我想出来了!这是我提出的:阵列中的结构......它看起来很好。

void save_game(fstream& file, player_type& pc)
{
    file.open("save.dat");
    if (file.is_open())
        for (int i = 0; i < 1; i++)
        {
            file << pc.w_name << endl;
            file << pc.d_rate << endl;
            file << pc.s_rate << endl;
            file.close();
        }
        else
            cout << " Error. Unable to open file.";
    menu(pc);
}//end save function

void continue_game(fstream& file, player_type player[])
{
    file.open("save.dat");
    if (file.is_open())
        for (int i = 0; i < 1; i++)
        {
            file >> player[i].w_name;
            file >> player[i].d_rate;
            file >> player[i].s_rate;
            file.close();
        }                   
        else
            cout << " Error. Unable to open file.";
    for (int i = 0; i < 1; i++)
    {
        cout << "You are continuing the game with the following weapon: " << player[i].w_name << endl;
        cout << "Which has a damage rating of " << player[i].d_rate << " and a stability rating of " << player[i].s_rate << endl; 
    }
    cout << endl;
    menu(pc);
}//end continue_game
.

其他提示

必须逐个读取和编写每个原始数据类型,仅使用未格式化的I / O。示例:

void serialize(std::ostream & o, Player const & p)
{
    o.write(reinterpret_cast<char const *>(p.type), sizeof p.type);
    o.write(reinterpret_cast<char const *>(p.damage), sizeof p.damage);
    o.write(reinterpret_cast<char const *>(p.stability), sizeof p.stability);
}

Player deserialize(std::istream & i)
{
    Player p;
    char pt;
    int pd, ps;

    if (!i.read(reinterpret_cast<char *>(&pt), sizeof pt)) { /* error */ }
    if (!i.read(reinterpret_cast<char *>(&pd), sizeof pd)) { /* error */ }
    if (!i.read(reinterpret_cast<char *>(&ps), sizeof ps)) { /* error */ }

    p.type = pt; p.damage = pd; p.stability = ps;
    return p;
}
.

以前的答案和编辑问题。

判断您的Continue_game签名,

int continue_game(ifstream &get)
.

PC必须是某种形式的全局结构,可容纳您尝试恢复的字符。如果在运行时分配PC,则您将使用它不正确。

当您这样做时:

    return pc.type;
    return pc.damage;
    return pc.stability;
.

功能只能有一个返回类型;只返回PC.Type。这不是错误的原因,而是您应该了解的设计缺陷。

您的初始方法以continue_game,其中您将相关结构传递为参数,

void continue_game(player_character &pc, ifstream &get_data) 
.

是一个更好的想法。我相信,如果你结合两者,你会发现自己没有错误。

如果错误继续,请使用调试器并粘贴该行。此外,如果您希望我,我可以提供代码的样本。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top