我有一个文本文件,如下所示。我希望读取文件,然后使用文件中的第二个整数。然而,我目前使用的代码只是接受第一个整数和字符串。虽然我想让它接受第二个整数和字符串。
所以我的问题是,这怎么可能?
getLine()
可以吗?我试图读取的文件和代码如下所示:
文件:10202 CE151 17.5
10105 CE151 99.9
10202 CE151 5.6
10406 CE301 59.8
10103 CE151 75.5
10709 CE204 67.2
代码:string mod;
float mark;
getline(file2, s2);
istringstream line(s2);
line >> mark;
line >> mod;
cout << mod << endl;
cout << mark << endl;
line >> reg;
line >> mod;
line >> mark;
cout << reg << endl;
cout << mod << endl;
cout << mark << endl;
第二个整数在第二行,所以需要跳过一行
(#include <limits> header for this)
file.ignore(std::numeric_limits<std::streamsize>::max(), 'n')
然后需要将整型读入变量
int number= 0;
file>> number;
现在你有第二个整数在number