为什么lseek返回0?
lseek() 应该返回文件描述符的位置。
文档说:
成功完成后,lseek() 返回结果偏移位置 以字节为单位测量 文件的开头。 否则,返回值-1 并设置 errno 来指示 错误。
问题是,即使这样也行不通:
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
printf("size off_t: %i\n", sizeof(off_t));
off_t pos;
pos = lseek(file, (off_t)0, SEEK_CUR);
printf("pos: %lli\n", pos);
// same result for SEEK_SET and SEEK_END
pos = lseek(file, (off_t)2352, SEEK_CUR);
printf("pos: %lli\n", pos);
这给了我:
size off_t: 8 pos: 0 pos: 0
这是为什么? 是否有其他方法可以使用原始 I/O 函数查找当前偏移量? (read
、open
、lseek
、...)
编辑 1:
我试图使示例更简单。
lseek()
is supposed to return the position of the file descriptor.
The documentation says:
Upon successful completion, lseek()
returns the resulting offset location
as measured in bytes from the
beginning of the file. Otherwise, a value of -1 is returned
and errno is set to indicate the
error.
Trouble is, not even this works:
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
printf("size off_t: %i\n", sizeof(off_t));
off_t pos;
pos = lseek(file, (off_t)0, SEEK_CUR);
printf("pos: %lli\n", pos);
// same result for SEEK_SET and SEEK_END
pos = lseek(file, (off_t)2352, SEEK_CUR);
printf("pos: %lli\n", pos);
This gives me:
size off_t: 8 pos: 0 pos: 0
Why is this? Is there an alternative to find the current offset, using the raw I/O functions? (read
, open
, lseek
, …)
Edit 1:
I tried to make the example simpler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试添加 #include到顶部。
请参阅:http://forums.macosxhints.com/archive/index.php /t-35508.html
基本上,由于您没有
#include
,编译器会“猜测”lseek()
返回一个 int。int 可能有 4 个字节长,并且由于 PPC 是“big-endian”字节顺序,因此您将获得“顶部”4 个字节,它们全部为零。
包含 unistd.h 可以让编译器意识到
lseek()
返回off_t
,因此您不会丢失一半字节。Try adding #include <unistd.h> to the top.
See: http://forums.macosxhints.com/archive/index.php/t-35508.html
Basically, since you didn't
#include <unistd.h>
, the compiler is "guessing" thatlseek()
returns an int.Probably an int is 4-bytes long, and since PPC is "big-endian" byte order, you're getting the "top" 4 bytes, which are all zero.
Include unistd.h lets the compiler realize that
lseek()
is returning anoff_t
, so you don't lose half the bytes.还有其他事情发生,可能是一些愚蠢的事情。 我尝试了您的代码,如下所示:
并得到以下结果:(
明确地说,这是在 Intel 上的 Mac OS/X 10.5.6 上。)
更新。
或者也许这并不愚蠢。 我刚刚在 PPC G5 上尝试过,并得到了你所做的结果。
更新 2
好的,这是 PPC 上的结果:
Something else is up, probably something silly. I tried your code, as here:
and get this result:
(Just to be definite, this is on Mac OS/X 10.5.6 on Intel.)
Update.
Or maybe it's not silly. I just tried it on a PPC G5, and get the results you do.
Update 2
Okay, here's the result on a PPC:
它是什么类型的文件? 它是管道吗? 因为如果它不是常规文件,很可能它不支持搜索:
What kind of file is it? Is it a pipe by any chance? Because if it's anything but a regular file, chances are it doesn't support seeking:
我不确定我是否理解你的问题,但这里有一些可能有帮助的想法。
——马库斯Q
I'm not sure I understand your question, but here are a few thoughts which might help.
-- MarkusQ
您可能希望将测试更改为:
您可能在某处达到 -1,但在这里测试为 0。
You might want to change the test to:
You are probably hitting a -1 somewhere, but you're testing for 0 here.