我真的很沮丧。我正在尝试读取具有以下内容的 txt 文件:
Arenas
Edward
239924731
2525976612
Autry
Richard
527646269
6028236739
使用此代码:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main(int argc, const char * argv[])
{
string line;
string last, first;
int uin, number;
ifstream phoneBook;
phoneBook.open("PhoneBook.txt");
while (!phoneBook.eof()) {
phoneBook >> last >> first >> uin >> number;
cout << last << ", " << first << ", " << uin << ", " << number <<endl;
}
return 0;
}
输出将是无休止的相同接触
Arenas, Edward, 239924731, 2147483647
Arenas, Edward, 239924731, 2147483647
Arenas, Edward, 239924731, 2147483647
Arenas, Edward, 239924731, 2147483647
Arenas, Edward, 239924731, 2147483647
我还尝试了这个版本的 while 循环:
while (phoneBook >> last >> first >> uin >> number) {
cout << last << ", " << first << ", " << uin << ", " << number <<endl;
}
由于某些原因,编译器甚至没有进入循环。它只会跳转到返回 0;