简体   繁体   English

ifstream.eof() - 在真实结束之前到达文件末尾

[英]ifstream.eof() - end of file is reached before the real end

I have a roughly 11.1G binary file where stores a series of the depth frames from the Kinect. 我有一个大约11.1G的二进制文件,其中存储了Kinect的一系列深度帧。 There are 19437 frames in this file. 此文件中有19437个帧。 To read one frame per time, I use ifstream in fstream but it reaches eof before the real end of the file. 要每次读取一帧,我在fstream中使用ifstream ,但它在文件的真实结束之前达到eof (I only got the first 20 frames, and the function stops because of the eof flag) (我只得到了前20帧,并且由于eof标志,函数停止了)

However, all frames can be read by using fread in stdio instead. 但是,可以使用stdio中的 fread来读取所有帧。

Can anyone explain this situation? 谁能解释这种情况呢? Thank you for precious time on 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;
}

Again, thank you for precious time on my question 再次感谢宝贵的时间在我的问题上

You need to open the stream in binary mode, otherwise it will stop at the first byte it sees with a value of 26. 您需要以二进制模式打开流,否则它将在其看到的第一个字节处停止,值为26。

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

As for why 26 is special, it's the code for Ctrl-Z which was a convention used to mark the end of a text file. 至于为什么26是特殊的,它是Ctrl-Z的代码,它是用于标记文本文件结尾的约定。 The history behind this was recorded in Raymond Chen's blog . 背后的历史记录在Raymond Chen的博客中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM