0

why is it that the last record of a binary file is being printed out twice?

while( (inFile)
{
    inFile.read(reinterpret_cast <char*> (&acc), sizeof(acc));  
    display(acc);
}

1 Answers1

2

Because your code should read

while (inFile.read(reinterpret_cast<char*>(&acc), sizeof(acc))
{
    display(acc);
}

Your version only tests for failure after you've printed the failed read. Or to put it another way while (infile) is not a test that the next read will succeed, it's a test that the last read succeeded.

john
  • 71,156
  • 4
  • 49
  • 68