【发布时间】:2013-02-08 15:37:49
【问题描述】:
我有一个大约 11.1G 的二进制文件,其中存储了来自 Kinect 的一系列深度帧。此文件中有 19437 帧。为了每次读取一帧,我在 fstream 中使用 ifstream 但它达到了 eof 在文件真正的结尾之前。 (我只得到了前 20 帧,由于 eof 标志,函数停止了)
但是,可以改为使用 stdio 中的 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;
}
再次感谢您在我的问题上花费宝贵的时间
【问题讨论】:
-
你以二进制模式打开C流,为什么不以二进制模式打开C++流?
ifstream depthStream("fileName.dat", std::ios_base::bin);(此外,每次迭代都删除和重新获取缓冲区似乎有点愚蠢,不是吗?并使用std::vector作为缓冲区。) -
另见:stackoverflow.com/questions/5605125/…。此外,您可能想使用
std::vector<char> buffer(size);之类的东西,而不是buffer = new char[size]; -
这个:
while(!depthStream.eof()总是错的。除了文件结尾之外,其他情况可能会导致读取失败。