【发布时间】:2014-12-24 08:03:13
【问题描述】:
我有一个文件,其内容如下所示:
Aadam
50
Aadam
0
Aad
0123
Waleed
12345
现在,我需要将第一行存储在字符串数组中,将第二行存储到整数数组中。 这是我写的源代码......
ifstream infile ("File.txt");
string name [20];
int score [20};
for (int i = 0; !infile.eof(); i++)
{
getline(infile, name[i]);
infile >> scores[i];
}
好吧,程序成功读取了第一行,但之后它什么也不做。 我首先尝试了另一种方法,首先将整数存储为临时字符串,然后使用“stoi”将该字符串转换为整数,这就像一个魅力。像这样:
for (int i = 0; !infile.eof(); i++)
{
getline(infile, name[i]);
string temp;
getline(infile, temp);
scores[i] = stoi(temp);
}
但问题是我不能使用 stoi。它在我的计算机上运行良好,但我必须将源代码提供给可能没有支持 C++11 的编译器的老师。这对我来说可能是个问题。 所以我需要另一种方式来从文件中输入数据。 所以,如果你知道如何做到这一点,请告诉我方法。
【问题讨论】:
-
还请注意这里写的内容:stackoverflow.com/questions/5605125/…