c++ - 为什么我不能成功读取文件数据到结构数组

标签 c++ struct deserialization

我正在为我的 C++ 期末编写一个基于战舰棋盘游戏的程序,我目前正在处理的问题是如何管理用户帐户。我必须允许用户每次玩游戏时都使用同一个帐户,并跟踪他们的输赢记录。我可以将数据写入文件,但我需要在玩家重新登录时将其读回,然后对其进行排序以找到他们的用户名。我卡在这部分了。

这是我正在使用的文件,其读作用户名、胜利、损失:

Rocky 0 0
Bob 0 0
dave 0 0
Jerry 0 0
Bert 0 0
Ernie 0 0
Marcus 0 0

编辑:这是我重复多次的输出

-858993460
-858993460

-858993460
-858993460

-858993460
-858993460

UserData 是一个结构体

//begin create/find account function
userData createAccount(userData ud){

    //local variables
    int playerOption;

    //creates object to open files
    ifstream infile;

    //creates object to open files
    ofstream outfile;

    do {
        cout << "Do you have an existing account?" << endl;
        cout << "" << endl;
        cout << "Enter 1 for yes or 2 for no:" << endl;
        cin >> playerOption;
    }
    while (playerOption >= 3 || playerOption <= 0);



    if (playerOption == 1){
        cout << "Enter user name:" << endl;
        cin >> ud.name;

        //opens file in read mode
        infile.open("userData.dat");

        //tests to make sure the file is open
        if (!infile){
            cout << "File open failure!";
        }


        //creates array of user data
        userData userDataArray [SIZE];

        //reads data from file into array until end of file
        int i=0;
        while(i<SIZE){
            infile >> userDataArray[i].name;
            infile >> userDataArray[i].wins;
            infile >> userDataArray[i].losses;
            i++;
        }

        ///test output
        int j=0;
        while (j<SIZE){
            cout << userDataArray[j].name << endl;
            cout << userDataArray[j].wins << endl;
            cout << userDataArray[j].losses << endl;
            j++;
        }
        //end test output


        //closes file
        infile.close();
    }

    else if(playerOption == 2){
        cout << "Enter user name:" << endl;
        cin >> ud.name;
        ud.wins = 0;
        ud.losses = 0;

        //opens file in write mode
        outfile.open("userData.dat",ios::app);

        //tests to make sure the file is open
        if (!outfile){
            cout << "File open failure!";
        }

        //writes userData struct to file
        outfile << ud.name << " " << ud.wins << " " << ud.losses << endl;

        //closes file
        outfile.close();
    }

    return ud;

//end create/find account function
}

最佳答案

这就是一个最小的例子,而且它完美地工作:

#include <iostream>
#include <fstream>

using namespace std;

typedef struct { string name; int wins; int losses; } userData;

void createAccount(){
    ifstream infile;
    infile.open("userData.dat");

    userData userDataArray[3];

    for(int i = 0; i < 3; ++i){
        infile >> userDataArray[i].name;
        infile >> userDataArray[i].wins;
        infile >> userDataArray[i].losses;
    }

    for(int i = 0; i < 3; ++i){
        cout << userDataArray[i].name << endl;
        cout << userDataArray[i].wins << endl;
        cout << userDataArray[i].losses << endl;
    }
}

int main(){
    createAccount();
}

输出:

Rocky
0
0
Bob
0
0
dave
0
0

所以你应该一点一点地简化你的代码,直到它工作。或者从像我这样的简单代码开始,逐步构建以实现所需的功能。

您最初的问题是“为什么我不能成功地将文件数据读取到结构数组”,但显然这不是问题所在。

首先,SIZE 的值是多少?如果您说输出很长,您可能将 size 设置为比文件中序列化数据的数量高得多的值,并且您正在打印大量未初始化的数据。

关于c++ - 为什么我不能成功读取文件数据到结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16154154/

相关文章:

java - Java从二进制文件中读取字符串

ios - 反序列化 Sendbird 消息的 CreatedAt 数据

struct - 返回对一个结构的部分的引用作为另一个结构的字段

c - 从二叉树中获取最大值

c++ - 是否可以转发声明命名空间内的 typedef?

c# - 通过 TCP 反序列化自定义类对象?

c++ - Qt DBus : register object that implements multiple interfaces

c++ - Win32 应用程序不在启动时运行

python - 在 C++ 中运行 python 对象

c++ - 修复没有安装 VS Redistributables 的 MFC 应用程序中的并排配置错误