C ++使用ifstream从dat文件加载数据
C++ loading Data from a dat file using ifstream
我正在为学校开展一个项目,其中我们要做的部分工作是将数据以团队名称后跟几个整数的形式保存到 .dat
文件中并检索该数据当节目是下一个 运行.
注意:计数器是一个变量,用于记录文件中存在的团队数量,在本例中设置为 3
void load(int counter)
{
ifstream infile("saveFile.dat");
string temp[25];
int temp2[25][8];
for (int i = 0; i < counter; i ++)
{
getline(infile, temp[i]);
for (int j = 0; j < 7; j++)
{
infile >> temp2[i][j];
}
}
cout << counter;
for (int i = 0; i < counter; i++)
{
string name = temp[i];
int gamesPlayed = temp2[i][0], wins = temp2[i][1], draws = temp2[i]
[2], losses = temp2[i][3],
goalsFor = temp2[i][4], goalsAgainst = temp2[i][5], points =
temp2[i][6];
CFootballTeam t(name, gamesPlayed, wins, draws, losses, goalsFor,
goalsAgainst, points);
table[i] = t;
}
}
这是我的加载函数的样子,但它似乎卡在了文件的第二行,returns 白色 space 后跟 -858993460 每个团队在第一个建议之后它没有读取任何数据,即使数据存在,这里是我的 saveFile.dat 文件的内容:
Manchester United
3 4 4 4 5 4 5
Manchester City
3 4 4 4 4 4 5
Chelsea
4 4 4 4 4 4 5
谁能告诉我如何让程序继续阅读其余行,而不仅仅是第 1 行和第 2 行?
谢谢
问题是您没有阅读您期望的内容。当您执行 getline()
时,程序会读取流中的所有内容,直到遇到 \n
,然后将其丢弃。当您执行 >>
时,它会一直读取直到达到 white-space,但会将 white-space 留在流中。这意味着当您完成第一次迭代时,一切都很好,但在最后 >>
时,您将离开 \n
字符。然后当你执行下一个 getline()
时,它会读入 \n
,给出一个空字符串,然后你对文本执行 >>
,你不再将所有内容都放在正确的变量中.您的输入看起来像这样:
Original Data
Manchester United\n
3 4 4 4 5 4 5\n
Manchester City\n
3 4 4 4 4 4 5\n
Chelsea\n
4 4 4 4 4 4 5\n
After getline()
3 4 4 4 5 4 5\n
Manchester City\n
3 4 4 4 4 4 5\n
Chelsea\n
4 4 4 4 4 4 5\n
After >>
\n
Manchester City\n
3 4 4 4 4 4 5\n
Chelsea\n
4 4 4 4 4 4 5\n
这意味着编译器可能会将值设置为某个默认值,在您的情况下,它看起来像是最小值。您需要做的是在每次迭代结束时添加 inFile.ignore()
以清除整数行末尾的 \n
。
我正在为学校开展一个项目,其中我们要做的部分工作是将数据以团队名称后跟几个整数的形式保存到 .dat
文件中并检索该数据当节目是下一个 运行.
注意:计数器是一个变量,用于记录文件中存在的团队数量,在本例中设置为 3
void load(int counter)
{
ifstream infile("saveFile.dat");
string temp[25];
int temp2[25][8];
for (int i = 0; i < counter; i ++)
{
getline(infile, temp[i]);
for (int j = 0; j < 7; j++)
{
infile >> temp2[i][j];
}
}
cout << counter;
for (int i = 0; i < counter; i++)
{
string name = temp[i];
int gamesPlayed = temp2[i][0], wins = temp2[i][1], draws = temp2[i]
[2], losses = temp2[i][3],
goalsFor = temp2[i][4], goalsAgainst = temp2[i][5], points =
temp2[i][6];
CFootballTeam t(name, gamesPlayed, wins, draws, losses, goalsFor,
goalsAgainst, points);
table[i] = t;
}
}
这是我的加载函数的样子,但它似乎卡在了文件的第二行,returns 白色 space 后跟 -858993460 每个团队在第一个建议之后它没有读取任何数据,即使数据存在,这里是我的 saveFile.dat 文件的内容:
Manchester United
3 4 4 4 5 4 5
Manchester City
3 4 4 4 4 4 5
Chelsea
4 4 4 4 4 4 5
谁能告诉我如何让程序继续阅读其余行,而不仅仅是第 1 行和第 2 行?
谢谢
问题是您没有阅读您期望的内容。当您执行 getline()
时,程序会读取流中的所有内容,直到遇到 \n
,然后将其丢弃。当您执行 >>
时,它会一直读取直到达到 white-space,但会将 white-space 留在流中。这意味着当您完成第一次迭代时,一切都很好,但在最后 >>
时,您将离开 \n
字符。然后当你执行下一个 getline()
时,它会读入 \n
,给出一个空字符串,然后你对文本执行 >>
,你不再将所有内容都放在正确的变量中.您的输入看起来像这样:
Original Data
Manchester United\n
3 4 4 4 5 4 5\n
Manchester City\n
3 4 4 4 4 4 5\n
Chelsea\n
4 4 4 4 4 4 5\n
After getline()
3 4 4 4 5 4 5\n
Manchester City\n
3 4 4 4 4 4 5\n
Chelsea\n
4 4 4 4 4 4 5\n
After >>
\n
Manchester City\n
3 4 4 4 4 4 5\n
Chelsea\n
4 4 4 4 4 4 5\n
这意味着编译器可能会将值设置为某个默认值,在您的情况下,它看起来像是最小值。您需要做的是在每次迭代结束时添加 inFile.ignore()
以清除整数行末尾的 \n
。