c fgetpos 给出错误的位置
我是 C 新手,我有以下代码:
f = fopen( argv[1], "rb" );
fseek( f, 64, SEEK_SET );
fpos_t pos;
fgetpos (f, &pos);
printf("%x", pos);
但是,这会返回 40,即使它应该返回 64。我做错了什么?
I am new to C and I have this code:
f = fopen( argv[1], "rb" );
fseek( f, 64, SEEK_SET );
fpos_t pos;
fgetpos (f, &pos);
printf("%x", pos);
However, this returns 40, even though it's supposed to be returning 64. What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在以十六进制格式输出 64,
"%x"
。既然64=0x40,谜团就解开了!You are outputting 64 in hex format,
"%x"
. Since 64=0x40, the mystery is solved!因为您正在使用 %x。它表示 40,即 0x40,即十六进制数。您需要 %i 或 %d 来获取十进制数。
Because you're using %x. It's saying 40 as in 0x40, the hexadecimal number. You need %i or %d to get a decimal number.
4 * 16 是 64
4 * 16 is 64
您打印的任何内容都是十六进制格式。 40十进制是64。
你的意思是文件大小是0x64还是0x40
whatever you are printing is in hex format. 40 in decimal is 64.
Do you mean the file size is 0x64 or 0x40
fpos_t
不是(不一定)算术类型,不能与printf
一起使用。如果愿意,实现甚至可以将其存储为包含加密位置的结构。使用ftell
(或ftello
,如果可用)以有意义的数字形式获取文件偏移量。fgetpos
基本上没用。fpos_t
is not (necessarily) an arithmetic type and cannot be used withprintf
. An implementation could even store it as a structure containing an encrypted position if it liked. Useftell
(orftello
if available) to get the file offset in a meaningful numeric form.fgetpos
is largely useless.