fread 在读取过程中以空值终止。还读取超出预期数据的垃圾
我正在使用 C++ 中的 FILE 对象读取二进制文件的各个部分。以下是 fseek 和相应的 fread 调用:
fseek(fp, startLocation, SEEK_SET);
fread(data, m_sizeOfData, 1, fp);
m_sizeOfData 最终成为大于 40 万的整数。看起来它应该将二进制文件中的所有 40 万多个字节读入数据(顺便说一句,这是一个 char[m_sizeOfData]),但是它在大约 6 或 7 个字符后停止在一个看起来像一个盒子的 unicode 字符处。我想这可能代表一个空终止?我对此并不乐观。我正在读取的文件的每个部分都不是这种情况。大多数似乎(通常)正确工作。
为什么会这样?有没有办法正确读取所有数据?
编辑:
fp 定义如下:
FILE* fp;
_wfopen_s(&fp, L"C://somedata.dat", L"rb");
此框字符(十六进制)为 0x06 后跟 0x00。
数据定义如下: char *data = new char[m_sizeOfData];
编辑2:
我还注意到另一个文件的末尾加载了一些垃圾。垃圾看起来像:
云云<<<<<<<<云云
这是因为它试图完成一定数量的字节吗
I am reading in pieces of a binary file using a FILE object in C++. Here is the fseek and corresponding fread call:
fseek(fp, startLocation, SEEK_SET);
fread(data, m_sizeOfData, 1, fp);
m_sizeOfData ends up being an integer greater than 400 thousand. This appears that it should read all 400 thousand+ bytes from the binary file into data (which is a char[m_sizeOfData], by the way), however it stops after about 6 or 7 characters at a unicode character that simply looks like a box. I'm thinking it might represent a null termination? I'm not positive on this. This isn't the case with every piece of the file that I am reading in. Most seem to work (generally) correctly.
Why might this be and is there a way to correctly read all of the data?
Edit:
fp is defined as such:
FILE* fp;
_wfopen_s(&fp, L"C://somedata.dat", L"rb");
This box character, in hex, is 0x06 followed by 0x00.
The data is defined by: char *data = new char[m_sizeOfData];
edit 2:
I've also noticed that another file is having some garbage loaded onto the end of it. The garbage looks like:
ýýýý««««««««îþ
Is this because it is trying to complete a certain round number of bytes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您以错误的方式使用 fread 的计数/大小参数。由于您正在读取字节,因此第二个参数应为 1,第三个参数应为计数:
然后您可以使用 fread 的返回值来确定读取了多少字节。如果您得到了预期的返回计数,那么您就可以放心地读取您想要的所有数据。在这种情况下,您可能错误地输出了数据 - 如果您将其视为以 NUL 结尾的字符串,那么您看到的 0x00 将是打印内容的结尾。 0x06 可能是盒子字形。
You are using the count/size parameters of fread the wrong way around. Since you are reading bytes, the second parameter should be 1 and the third parameter the count:
You can then use the return value of fread to determine how many bytes were read. If you are getting the expected count returned, then you can be comfortable that you are reading all the data you wanted. In this case, you are probably outputting the data incorrectly - if you are treating it as a NUL-terminated string, then the 0x00 you are seeing will be the end of what is printed. The 0x06 is probably the box glyph.
你怎么知道它停在你说的地方?如果您只是使用字符串函数查看结果,这些字符串函数将全部停止在第一个空字符处 - 实际数据可能会进一步扩展。
How do you know it's stopping where you say? If you're just looking at the result with string functions, those string functions will all stop at the first null character - the actual data might extend much much further.
如果你在 Windows 上,我认为有一些字符,如 ctrl-Z 或 ctrl-D 可以表示文件结束,除非你专门以二进制模式打开文件
If you are on windows, I think there are some chars that like ctrl-Z or ctrl-D that can signify end of file unless you specifically open the file in binary mode