为什么我不能成功地将文件数据读取到结构数组
Why can't I succesfully read file data to a struct array
我正在为我的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是struct
//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
设置为比文件中序列化数据的数量大得多的值,并且您正在打印大量未初始化的数据。
相关文章:
- 如何在ros2中从rosbag2读取数据
- OpenCV 和从 OutputArrays 或 (Mat) 读取数据
- 使用C++从磁卡读卡器MT188读取数据
- HDF5 简单读取数据集失败
- 嵌套结构,从由空行分隔的文件中读取数据
- 不要从输入队列套接字读取数据
- 从文件中读取数据,操作员>>
- 如何从excel文件中读取数据并存储到变量中?
- C++:从文件中读取x y数据会产生无限循环吗
- 无法通过FT232RL读取数据
- 如何从QNetworkReply读取数据?
- C++ 逐行从文件(包含空格)读取数据
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- 我们可以从 TXT 文件中读取数据并使用 C 和 C++ 将其保存到 SQL 数据库吗?
- 从多个 tcp 连接读取数据
- 从文本文件中读取数据并删除所有换行符空格,并在 C++ 控制台中显示
- Qt TCP服务器不从客户端读取数据
- 累积从 C++11 中的文件中读取数据的整数
- 从 txt 文件中读取数据的最简单方法
- 从 txt 文件中读取数据的 Getline