我有一个198的文件(但它可以在0-200之间)。我文件的每一行都是这样的:
Urdnot Wrex,2.75,198846.13
Urdnot Bakara,3,189484.84
Hannah Shepard,1.75,188145.14
David Anderson,2.25,182169.46
Kasumi Goto,2.75,176795.83
这是我的代码,但是,它不想工作。
int index = 0; // The index of the file.
while(index <= 200) {
in.ignore(256, ',');
in >> employeeName;
in.ignore(256, ',');
in >> employeeScore;
in.ignore(256, '\n');
in >> employeeSalary;
cout << index << ": " << employeeName << ", " << employeeScore << ", " << employeeSalary << endl;
index++;
}
但是,对于198行的文件,它只读取3,输出为:
0: 2.75,198846.13, 3, 0
1: 2.75,198846.13, 3, 0
2: 2.75,198846.13, 3
如果有人对如何使其发挥任何想法,那将非常感激。
答案 0 :(得分:1)
像这样:
#include <fstream>
std::ifstream infile("thefile.txt");
if (infile.is_open()) {
float number;
std::string str;
char c;
while (infile >> str >> >> c >> number && c == ',')
cout << number << " " << str << "\n";
}
infile.close();