Here is my code:
#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 content:
5 9 2 9 3
8 2 8 2 1
0
In Enclipse, it goes to infinite-loop. I’m using MinGW 5.1.6 + Eclipse CDT.
I tried many things but I couldn’t find the problem.
Since you are on windows try:
The last line is stored as
"0\r\n". The\nis used as the line delimiter by getline so the actual line read will be"0\r"or
you can convert the dos format file to UNIX format using command
Now your original program should work. The command will change the
\r\nat the end of the line to\nAlso you should always do error checking after you try to open a file, something like: