为什么我在 C 语言中使用 fread() 没有得到预期的结果?
这是我的代码:
#include <stdio.h>
int main(void) {
FILE *fp;
unsigned int i;
char bytes[512];
fp = fopen("myFile","r");
for(i = 0;i <= 512;i++) {
fread(&bytes, sizeof(bytes), 1, fp);
printf("bytes[%d]: %x\n", i, bytes[i]);
}
}
这是预期的输出
$ hexdump myFile
0000000 aa55 aa55 0060 0000 0a17 0000 b1a5 a2ea
0000010 0000 0000 614c 7563 616e 0000 0000 0000
0000020 0000 0000 0a68 0000 1001 421e 0000 0000
0000030 f6a0 487d ffff ffff 0040 0000 002f 0000
但是这是我从程序中看到的内容
bytes[0]: 55
bytes[1]: 8
bytes[2]: ffffffc8
bytes[3]: ffffffdd
bytes[4]: 22
bytes[5]: ffffffc8
bytes[6]: ffffff91
bytes[7]: 63
bytes[8]: ffffff82
我明显的猜测是我要么错误地处理了某些内容并收到了错误的数据,要么我错误地打印了它并以错误的方式查看它。
Here is my code:
#include <stdio.h>
int main(void) {
FILE *fp;
unsigned int i;
char bytes[512];
fp = fopen("myFile","r");
for(i = 0;i <= 512;i++) {
fread(&bytes, sizeof(bytes), 1, fp);
printf("bytes[%d]: %x\n", i, bytes[i]);
}
}
Here is the expected output
$ hexdump myFile
0000000 aa55 aa55 0060 0000 0a17 0000 b1a5 a2ea
0000010 0000 0000 614c 7563 616e 0000 0000 0000
0000020 0000 0000 0a68 0000 1001 421e 0000 0000
0000030 f6a0 487d ffff ffff 0040 0000 002f 0000
But here is what I see from my program
bytes[0]: 55
bytes[1]: 8
bytes[2]: ffffffc8
bytes[3]: ffffffdd
bytes[4]: 22
bytes[5]: ffffffc8
bytes[6]: ffffff91
bytes[7]: 63
bytes[8]: ffffff82
My obvious guess is that I'm either addressing something incorrectly and receiving the wrong data back or I am printing it incorrectly and viewing it the wrong way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每次循环时,您都会从文件中读取连续的 512 字节块,并仅打印每个块的一个字节。您可能想一次性读取这 512 个字节,然后打印它们,如下所示:(
另外:一些错误检查不会出错,正如 Dav 指出的那样,您应该检查您是否确实正在读取预期数量的文件中的字节。)
You're reading successive 512-byte chunks from your file each time round the loop, and printing only one byte of each chunk. You might want to read those 512 bytes in one go, then print them afterwards, like this:
(Also: some error checking wouldn't go amiss, and as Dav points out, you should check that you really are reading the expected number of bytes from the file.)
RichieHindle 已经解决问题的主要部分,现在我想帮助解决次要部分。
当您打印结果时,您的
char
会被扩展为int
。对于正数,这并不重要,但对于负数(任何 >= 0x80),您会在开头插入一堆符号位。很容易修复:RichieHindle has already solved the major part of the problem, now I'd like to help with the minor part.
When you're printing the results, your
char
s are being extended toint
s. For a positive number this doesn't matter, but for a negative number (anything >= 0x80) you get a bunch of sign bits inserted at the beginning. It's easy to fix:附加说明。循环中存在差一错误,您正在打印 513 个元素。
Additional note. There is an off-by-one error in the loop, you are printing 513 elements.
几条评论:
1)您在循环中执行了 fread 512 次,每次读取 512 字节。将fread移出循环,一次性读取,或者如果想在循环中读取,则将参数2将fread更改为sizeof int。
2)您没有检查实际读取的字节数或文件是否已成功打开。
Couple of comments:
1) You have fread in a loop executing 512 times and you are reading 512 bytes each time. Move the fread out of the loop and read it in one go or change parameter 2 to fread to the sizeof int if you want to read it in the loop.
2) You are not checking the number of bytes actually read or if the files was successfully opened.