Ifstream.eof () - end of file reached to real end

I have an approximately 11.1G binary file that stores a series of depth frames from Kinect. This file has 19437 frames. To read one frame at a time, I use ifstream in fstream , but it reaches eof to the real end of the file. (I only got the first 20 frames, and the function stops due to the eof flag)

However, all frames can be read using fread in stdio .

Can someone explain this situation? Thanks for the precious time to my question.

Here are my two functions:

// ifstream.read() - Does Not Work: the loop will stop after 20th frame because of the eof flag ifstream depthStream("fileName.dat"); if(depthStream.is_open()) { while(!depthStream.eof()) { char* buffer = new char[640*480*2]; depthStream.read(buffer, 640*480*2); // Store the buffer data in OpenCV Mat delete[] buffer; } } // fread() - Work: Get 19437 frames successfully FILE* depthStream depthStream = fopen("fileName.dat", "rb"); if(depthStream != NULL) { while(!feof(depthStream)) { char* buffer = new char[640*480*2]; fread(buffer, 1, 640*480*2, depthStream); // Store the buffer data in OpenCV Mat delete[] buffer; } 

Thanks again for the precious time to my question.

+7
source share
1 answer

You need to open the stream in binary mode, otherwise it will stop in the first byte that it sees with a value of 26.

 ifstream depthStream("fileName.dat", ios_base::in | ios_base::binary); 

As for why 26 is special, this is the code for Ctrl-Z, which was the symbol for the end of a text file. The story behind this was recorded on Raymond Chen's blog .

+13
source

All Articles