wil*_*een 9 c++ ifstream fread
我有一个大约11.1G的二进制文件,其中存储了Kinect的一系列深度帧.此文件中有19437个帧.要每次读取一帧,我在fstream中使用ifstream,但它在文件的真实结束之前达到eof.(我只得到了前20帧,并且由于eof标志,函数停止了)
然而,所有的帧可以通过读取FREAD在标准输入输出来代替.
谁能解释这种情况呢?感谢您在我的问题上花费宝贵的时间.
这是我的两个功能:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
再次感谢宝贵的时间在我的问题上
Mar*_*som 17
您需要以二进制模式打开流,否则它将在其看到的第一个字节处停止,值为26.
ifstream depthStream("fileName.dat", ios_base::in | ios_base::binary);
Run Code Online (Sandbox Code Playgroud)
至于为什么26是特殊的,它是Ctrl-Z的代码,它是用于标记文本文件结尾的约定.背后的历史记录在Raymond Chen的博客中.
归档时间: |
|
查看次数: |
5401 次 |
最近记录: |