Reading multiple lines from a file using getline()
Asked Answered
C

1

9

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.

Cullan answered 24/12, 2013 at 7:47 Comment(1)
You're throwing out your getline() results with the first extraction to firstName immediately after. You can start there. Regarding a sample of getline(), there's one in the online-docs.Fledgy
C
13

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.

Carousal answered 24/12, 2013 at 7:49 Comment(2)
Abandoning getline entirely, as you suggest here, actually makes a lot of sense, since the only reason I was using it to begin with was to read in the space between firstName and lastName, but I end up concatenating the two anyway! But I did not know the extraction could be used as condition for the while loop. Thanks for the useful insight and suggestion!Cullan
@Abhishek Bansal - Why can't I find the statement "it returns a boolean value …" in the linked getline reference?Unteach

© 2022 - 2024 — McMap. All rights reserved.