the end of binary file

Hi all,
I'm using fstream object to read a binary file, but I don't know if it reachs the end of the file, because the eof() function doesn't work for binary files. I always read one more time from the file.

That is to say, if I just write one int value to the file, and then I read it. but as a result, I entered the following loop two times. I know the read() fuction consider EOF as a valid content, but I have no solution. So my problem is how to decide the end of binary files

while(!infile.eof())
{
    infile.read(...);    // read one int value
}

Thanks.



Answer this question

the end of binary file

  • ds12will

    I usually read a temp buffer of a specific size and append that buffer to a master buffer. if the number of bytes read is smaller than the temp buffer size, end of file has been reached.

  • Tyler Frugia

    is the feof() fuction available for C++ fsream object
  • King Gamo

    Thanks,

    ofcourse it works, I wonder if there is some function can judge the end of binary file. I noticed that C run time library function feof() can do it, but how about C++ fstream objects

  • AuzzieFlyBoy

    feof() instead of eof()



  • the end of binary file