<分区>
我有一个名为 scores.txt 的文本文件,其中包含以下数据:
John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
我正在尝试从此文件中读取数据并使用以下 C++ 代码打印它:
ifstream input;
input.open("scores.txt");
string firstName;
char mi;
string lastName;
int score;
while (input) {
input>>firstName>>mi>>lastName>>score;
cout<<firstName<<" "<<mi<<" "<<lastName<<" "<<score<<endl;
}
input.close();
cout<<"Done"<<endl;
输出是:
John T Smith 90
Eric K Jones 103
Ram S Krishnan 25
Marie A Bell 50
Marie A Bell 50
为什么最后一行 (Marie A Bell 50) 被打印了两次?我怎样才能防止这种情况发生?