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);
}
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);
}
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.