【发布时间】:2016-02-17 17:31:58
【问题描述】:
我正在尝试读取以下格式的二进制文件:
-64 位整数
-3276 32 位浮点数
-(重复最后两行直到 eof)
这是我解释文件的块:
ifstream bbrFile;
ofstream csvFile;
bbrFile.open(inFilename);
csvFile.open(dataFilename);
//yes I did actually check to make sure that the files had opened.
//I omitted it here for brevity
long long int time;
float point;
while (bbrFile)
{
bbrFile.read((char*)&time, sizeof(time));
csvFile << time;
for (int i = 0; i < 3276; i++) {
bbrFile.read((char*)&point, sizeof(point));
csvFile << ',' << point;
}
csvFile << "\n";
}
到目前为止,我的代码工作正常,只是它认为在读取大约 53 个浮点数后它已到达文件末尾,然后只输出它读取的最后一个浮点数,直到“for”循环结束。我尝试使用 fread 和 FILE* 而不是 read 和 fstream,得到了相同的结果。我也试过替换
while (bbrFile)
与
while (!bbrFile.eof())
无济于事。
由于二进制文件大约有 12 兆字节,我有点不知道为什么它在这里停止读取。
【问题讨论】:
-
什么是
trace?这在这里似乎很重要。 -
这是我从自己的代码中复制时犯的错误。
trace应改为point。