Reading multiple lines from a file using getline()

Ghost Repeater picture Ghost Repeater · Dec 24, 2013 · Viewed 83.8k times · Source

I am trying to read in and then output the contents of a text file with three lines, as follows:

Bob Dylan 10 9

John Lennon 8 7

David Bowie 6 5

For each line, I just want to output the line, i.e. firstName LastName number1 number2.

I'm using the following code for this:

int num1;
int num2;
string firstName;
string lastName;
string fullName; 
ifstream inFile;

inFile.open("inputFile.txt");

while (getline(inFile, firstName))
    {
        inFile >> firstName >> lastName >> num1 >> num2;

        fullName = firstName + " " + lastName;

        cout << fullName << " " << num1 << " " << num2 << endl;
    }

inFile.close();

There are 2 problems with the output from this. First, the first line is not output, although from experimentation I know that it DOES read it in. Second, after the last 2 lines are read in and output (as desired), the program displays everything in the last line EXCEPT the first name (in this case the last thing it prints is Bowie 6 5).

Can someone use this simple example to explain how the getline function works when reading in multiple lines from a file? (I don't even know if it's the best way, but it's the only way I know as of yet). Here are some specific questions.

First, does the while loop conditional getline(inFile, firstName) return a boolean? If so, how can it be true (i.e. how can the while loop start) if I haven't given firstName a value yet? Is it the case that the program reads the first line and if there's something there, then it executes the while loop, but starting with the second line, because it already used the first to check for content?

Second, if firstName does have a value, and if that value is the first name on the first line ("Bob" in this case), why isn't the first line output at all? I've been racking my brain trying to figure out where it went to.

Third, after the program reads in and displays the last two lines, the program moves to the next line and encounters nothing but blanks, right? Then what would be the value of firstName? Would it be blank, or would it still be "David"? If it's blank, why does the while loop execute again? But if it's "David", then why does the program not output that value along with the others?

Btw, I am working out of a textbook (not for homework), and it covers getline, but not for multiple lines. But then the exercises involve multiple lines, so I'm a bit lost.

Answer

Abhishek Bansal picture Abhishek Bansal · Dec 24, 2013

You are trying to read each line twice.

while (getline(inFile, firstName)) // reads the line
    {
        // reads the next line and overwrites firstName!
        inFile >> firstName >> lastName >> num1 >> num2;

Change it to:

while ( inFile >> firstName >> lastName >> num1 >> num2 )
{
    fullName = firstName + " " + lastName;
    cout << fullName << " " << num1 << " " << num2 << endl;
}

EDIT: To answer your questions:

How does getline() work?
Reads the entire line up to '\n' character or the delimiting character specified. http://www.cplusplus.com/reference/string/string/getline/?kw=getline

After reading the line, the control goes to the next line in the file.
Also, it returns a boolean value of true if the read operation was successful, else false.

The extraction operator truncates on all whitespaces by default. It also returns a boolean value indicating whether the operation was successful.