问题描述
我有一个大约11.1G的二进制文件,其中存储了Kinect的一系列深度帧。此文件中有19437帧。要每次读取一帧,我在 fstream 中使用 ifstream ,但它达到了 eof 。 (我只有前20帧,并且由于 eof 标志而停止了功能)
I have a roughly 11.1G binary file where stores a series of the depth frames from the Kinect. There are 19437 frames in this file. To read one frame per time, I use ifstream in fstream but it reaches eof before the real end of the file. (I only got the first 20 frames, and the function stops because of the eof flag)
但是,可以使用 stdio 中的 fread 来读取所有帧。
However, all frames can be read by using fread in stdio instead.
有人可以解释这种情况吗?谢谢您宝贵的时间回答我的问题。
Can anyone explain this situation? Thank you for precious time on my question.
这是我的两个功能:
// 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
推荐答案
您需要以二进制模式打开流,否则它将停止在它看到的第一个字节处,值为26。
You need to open the stream in binary mode, otherwise it will stop at the first byte it sees with a value of 26.
ifstream depthStream("fileName.dat", ios_base::in | ios_base::binary);
至于为什么26很特别,这是Ctrl-Z的代码,用来标记文本文件的末尾。背后的历史记录在雷蒙德·陈的博客。
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. The history behind this was recorded in Raymond Chen's blog.
这篇关于ifstream.eof()-在实际结尾之前到达文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!