tags:

views:

374

answers:

3

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.

+2  A: 

It will create an infinite loop if no line contains exactly 0. For example 0\n is not the same thing as 0. My guess is that that is your problem.

EDIT: To elaborate, getline should be discarding the newline. Perhaps the newline encoding of your file wrong (i.e. windows vs. unix).

Turtle
Just to clarify - an input file that ends in `0\n` should be fine, but there will be problems as you describe if the input file has whitespace (other than `\n`) after the `0`. So `'0 \n'` will fail and `'0\r\n'` will fail, which might be caused by the Unix vs DOS line ending encoding problem.
Michael Burr
I'm running them on Windows. Same text file and code running on Dev C++ but fails on Eclipse.
pocoa
Thank you. Well, as you see, my mistake was more newbie :)
pocoa
+2  A: 

Since you are on windows try:

} while (line != "0\r");

The last line is stored as "0\r\n". The \n is 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

dos2unix input.txt

Now your original program should work. The command will change the \r\n at the end of the line to \n

Also you should always do error checking after you try to open a file, something like:

inputFile.open("input.txt");
if(! inputFile.is_open()) {
 cerr<< "Error opening file";
 exit(1);
}
codaddict
You're right. When I changed the code to:inputFile.open("C:\\workspace\\project\\input.txt");it works. But why? input.txt is located in the same folder. What is the difference between Dev C++ and Eclipse?
pocoa
@pocoa: the current directory is different. Eclipse CDT lets you specify this on the project's "Run/Debug settings" properties for the project (on the "(x)= Arguments" tab). I have no experience with Dev C++, so I don't know where you'd go to specify the working directory for running the built program.
Michael Burr
@Michael Burr: Do you know how to do that?
pocoa
@pocoa: unfortunately, no more than what I already put in the previous comment.
Michael Burr
@Michael Burr: Thank you anyway.
pocoa
+1  A: 

Your main problem is working directory.
Because you are specifying a file using a relative path it searches for the file from the current working directory. The working directory can be specified by your dev environment. (Note: The working directory is not necessarily the same directory where the executable lives (this is a common assumption among beginners but only holds in very special circumstances)).

Though you have a special end of input marker "0" you should also check that the getline() is not failing (as it could error out for other reasons (including beady formatted input). As such it is usually best to check the condition of the file as you read it.

int main()
{
    string   line;
    ifstream inputFile;
    inputFile.open("input.txt");

    while((getline(inputfile, line)) && (line != "0"))
    {
        // loop only entered if getline() worked and line !="0"
        // In the original an infinite loop is entered when bad input results in EOF being hit.

        cout << line << endl;
    }
    if (inputfile)
    {
        cout << line << endl; // If you really really really want to print the "0"
                             // Personally I think doing anything with the termination
                             // sequence is a mistake but added here to satisfy comments.
    }

    return 0;
}
Martin York
Your program won't print the last line.
Turtle
True: But I assumed that was a programming mistake by poca as you don't usually want to print the data termination sequence it has no meaning in terms of data. But it is easy to fix.
Martin York
Yeah, the code was crappy but the problem was not there.
pocoa