【发布时间】:2010-03-30 05:34:12
【问题描述】:
这是我的代码:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main() {
string line;
ifstream inputFile;
inputFile.open("input.txt");
do {
getline(inputFile, line);
cout << line << endl;
} while (line != "0");
return 0;
}
input.txt 内容:
5 9 2 9 3
8 2 8 2 1
0
在 Enclipse 中,它进入无限循环。我正在使用 MinGW 5.1.6 + Eclipse CDT。
我尝试了很多东西,但我找不到问题。
【问题讨论】:
-
如果指定“input.txt”的绝对路径而不是相对路径会怎样?
-
它进入一个无限循环,因为您没有在条件下检查文件的状态。您应该检查文件的状态(看看它是否变坏了)并检查终止顺序。请参阅下面的答案。