Reading from Text File Until Eof Repeats Last Line

Reading from text file until EOF repeats last line [duplicate]

Just follow closely the chain of events.

  • Grab 10
  • Grab 20
  • Grab 30
  • Grab EOF

Look at the second-to-last iteration. You grabbed 30, then carried on to check for EOF. You haven't reached EOF because the EOF mark hasn't been read yet ("binarically" speaking, its conceptual location is just after the 30 line). Therefore you carry on to the next iteration. x is still 30 from previous iteration. Now you read from the stream and you get EOF. x remains 30 and the ios::eofbit is raised. You output to stderr x (which is 30, just like in the previous iteration). Next you check for EOF in the loop condition, and this time you're out of the loop.

Try this:

while (true) {
int x;
iFile >> x;
if( iFile.eof() ) break;
cerr << x << endl;
}

By the way, there is another bug in your code. Did you ever try to run it on an empty file? The behaviour you get is for the exact same reason.

Repeated value at the end of the file [duplicate]

This is wrong

while(!inFile.eof())
{
inFile>>grades;
cout<<grades<<endl;
}

This is right

while (inFile >> grades)
{
cout << grades << endl;
}

Must be the single most common error on this forum. eof() does not tell you that the next read will have an end of file error, it tells you that the last read failed because of end of file. So if you must use eof() you should use it after you read not before.

Why is the last line of my input file running twice?

The problem is that eof does not do what you think it does.

Imagine you are walking on a floor, tile after tile, picking up what's on the floor, putting it in your pockets and show up (print) your pockets content.

When you put you feet on the last tile the floor is not yet "ended" and your nose is still safe. You have not (yet) smashed the wall. You fill-up the pockets and print them.

eof,then, tells you when your nose is broken, not when the tile is the last.

So, you are on the last tile, check your nose, find it ok, and go one step forward. Your nose is now bleeding, nothing is there to peek up to put in our pockets, and your pockets still contain ... what they had before.

You print the content of your pocket (once again) and than check your nose. It's broken: you exit.

The idiomatic way to solve that problem is this one:

while(inFile >> lastName
>> firstName
>> salary
>> percent)
{
//all your computation here
}

I think you should understand by yourself why.

EOF, looping forever when the last line is read. c++

Somehow this question manages to get asked about 100 different ways, none of which is quite a duplicate of the others. Worse, every time it gets asked, it gets a ridiculous number of bad answers.

To read the data correctly from a stream, you want a loop with a single condition that reads the data and checks whether that attempt at reading succeeded. Almost anything else is prone to failure. In particular, it's nearly impossible to get a loop of the form:

while (!foo.eof())

or:

while (!foo.bad())

or:

while (foo.good())

or:

while (file)

...to actually work correctly. Every one of these is fundamentally broken. The easy way to get correct results is to combine reading and testing into a single operation, such as:

while (std::getline(somefile, somestring))

or:

while (file >> a >> b >> c)

As to why these work and the others don't: end of file is not detected immediately upon the file pointer reaching the end of the file. Rather, it's detected when you attempt to read something, but the file pointer is already at the end of the file.

So, what happens is that when at the point that you've read all the data in the file, file.eof() remains false. You attempt to read the next item from the file (and it fails). Then you attempt to process the data even though the read failed.

The reason the forms I recommend work correctly is that they attempt to read data, then check whether that succeeded, and immediately exit the loop if the attempted read failed, so they do not attempt to process data after a read attempt failed.

As a sort-of separate (but closely related) point: if you're trying to read an entire file, you do not normally want to base exiting the loop specifically on detecting end of file. You want to read as long as you can convert input, and exit the loop as soon as that fails.

These are different when/if you encounter some other error before reaching the end of the file. In such a case, a loop that waits specifically for end of file can (and often will) continue running indefinitely, repeatedly attempting an operation that has already failed, but failed in a way that the loop isn't written to detect correctly.

The time to use file.eof() is after you've exited from the loop, and want to detect why the loop exited. If you've reached eof, great--you just read all the data. If you expected the loop to read the entire file, but it exited before reaching eof, then you probably have a problem -- perhaps a problem with the disk drive, or perhaps just bad data in the file. You can typically use file.eof, file.fail and file.bad to sort out what sort of error you encountered (if any).

Program Reading Last Line of File Twice [duplicate]

while(!feof(textFile))

is wrong, you end up "eating" the end of file. You should do

while(fgets(buffer, MAX_LINELENGTH, textFile))
{
// process the line
}

Related: Why is iostream::eof inside a loop condition considered wrong?

Read multiple line in text file and write in another text file

I think your problem is this line

while(!scores.eof()){

When you have read the last line, scores.eof() is false, you try to read another line, the reading fail (and scores.eof() become true), you don't test if the reading is with or without error and you use two times the values of the last line.

I suggest something like

while( scores >> over >> maiden >> runs >> wickets ){
avg = runs/float(wickets);


Related Topics



Leave a reply



Submit