使用函数将结构写入/读取到文件,Visual C++ 2008 express

write/read struct to file using functions , Visual C++ 2008 express

本文关键字:文件 Visual C++ express 2008 读取 函数 结构      更新时间:2023-10-16

我使用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功能似乎有效。我(还没有(收到任何错误。当我在制作角色后选择保存时,我收到以下错误:在撤消.exe中0x69197a28未处理的异常: 0xC0000005:访问冲突读取位置0xffffffcc。

谷歌将其显示为某种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代码并将其包含在此处。我还收集了调试器和自动输出(自动的小版本,不是全部七页(。显然,我的值没有在save_game中进行评估,因此continue_game没有什么可处理的,也不会导致错误。

以下是代码和调试器/自动打印输出:

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

调试器输出窗口:撤消.exe中0x644e7a28的首次机会异常: 0xC0000005:访问冲突读取位置0xffffffcc。撤消.exe:0xC0000005:访问冲突读取位置0xffffffcc 0x644e7a28处未处理的异常。

汽车:- pc {type='Ì' damage=-858993460 stability=-858993460 } player_character & 类型 -52 "Ì"字符 伤害 -858993460 int 稳定性 -858993460 int 电脑类型 -52 'Ì' 字符- 保存 {_Filebuffer={...} } std::basic_ofstream> &+ 标准::basic_ostream> {...} 标准::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代码仍然无法正常工作,但它确实会打印到屏幕上。

您必须逐个读取和写入每个基元数据类型,并且仅使用无格式的 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) 

是一个更好的主意。我相信,如果你将两者结合起来,你会发现自己没有错误。

如果错误仍然存在,请使用调试器并粘贴该行。此外,如果您愿意,我可以提供我描述的代码示例。

看来我想通了!这是我想出的:数组中的结构...它似乎工作得很好。

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