简体   繁体   中英

Problems reading first line from file after 'getline'

After inputting getline , the first row of numbers in my input file are ignored and the program starts at the second line until the end.

Here's the code:

while (!file.eof())
{
    getline(file, lineBuffer);
    if(lineBuffer.length()==0)
    {
        continue; //ignore empty lines
    }
    else 
    {
       // file.open("test.txt");
        while (file >> A >> B >> N)
        {

            for(int count=1; count<=N; count++)
            {
                if(count%A == 0 && count%B == 0) { cout << "FB "; }

                else if(count%A == 0) { cout << "F "; }

                else if(count%B == 0) { cout << "B "; }

                else { cout << count << " "; }

            }
            cout << endl;
        }
    }
}

The input file contains 3 rows of integers and only the second line and third like are processed. I've looked this up extensively, but no one has mentioned a case similar to mine. Usually they deal with people trying to get user input after a getline function. Any ideas would be greatly appreciated.

getline(file, lineBuffer) reads a line from file and stores it in lineBuffer . If you want to use it, you should then process lineBuffer instead of reading from file (where the first line will already be skipped). To make it work, simply remove all code outside of the inner while . This will read from the file 3 numbers at a time, stopping when extracting one of them fails.

Is the above code real or is it an example?

If it is real, you don't need to skip th eempty lines. The operator>> will skip the whitespaces for you.

The following code without any lineskipping performs just the same reading of "ABN" lines:

// outer loop is not needed, either remove it, or include error checking
// while (!file.eof() && !file.fail())
{
    while (file >> A >> B >> N)
    {
        for(int count=1; count<=N; count++)
        {

            if(count%A == 0 && count%B == 0) { cout << "FB "; }
....

If for some reason you cannot remove the getline at all, remember that getline reads the line ALWAYS (well, unless the streampointer reached its end). This means that your code skips the empty lines, then reads first nonempty line, notices that the line is not empty and breaks the loop. Note that it has read the line , hence, your first-line-of-data now is in the linebuffer , and not in the stream anymore. Now you have to either unget the whole line (if your stream supports it) or rewind the stream by the line's length (if your stream supports it) or - just read the data from the line - for example using istringstream class.

In general, mixing getline and operator>> is doable but tricky, (especially when you first read via >> and then want to skip a 1 or 2 lines), as they behave a little differently in terms of buffering and whitespace-handling. If you really want that, search for this subject - there are lots of examples how to sync them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM