为什么lseek返回0?

发布于 2024-07-14 08:16:15 字数 908 浏览 6 评论 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 函数查找当前偏移量? (readopenlseek、...)

编辑 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

隐诗 2024-07-21 08:16:15

尝试添加 #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" that lseek() 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 an off_t, so you don't lose half the bytes.

柠檬色的秋千 2024-07-21 08:16:15

还有其他事情发生,可能是一些愚蠢的事情。 我尝试了您的代码,如下所示:

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
    off_t pos;
    int file ;
    if((file = open("/Users/chasrmartin/.bash_history",O_RDONLY)) == -1){
        perror(argv[0]);
        exit(1);
    }
    printf("size off_t: %i\n", sizeof(off_t));

    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);

    exit(0);
}

并得到以下结果:(

bash $ gcc foo.c
bash $ ./a.out
size off_t: 8
pos: 0
pos: 2352

明确地说,这是在 Intel 上的 Mac OS/X 10.5.6 上。)

更新。

或者也许这并不愚蠢。 我刚刚在 PPC G5 上尝试过,并得到了你所做的结果。

更新 2

好的,这是 PPC 上的结果:

$ gcc foo.c
$ ./a.out
size off_t: 8
pos: 0
pos: 0

Something else is up, probably something silly. I tried your code, as here:

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
    off_t pos;
    int file ;
    if((file = open("/Users/chasrmartin/.bash_history",O_RDONLY)) == -1){
        perror(argv[0]);
        exit(1);
    }
    printf("size off_t: %i\n", sizeof(off_t));

    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);

    exit(0);
}

and get this result:

bash $ gcc foo.c
bash $ ./a.out
size off_t: 8
pos: 0
pos: 2352

(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:

$ gcc foo.c
$ ./a.out
size off_t: 8
pos: 0
pos: 0
浅紫色的梦幻 2024-07-21 08:16:15

它是什么类型的文件? 它是管道吗? 因为如果它不是常规文件,很可能它不支持搜索:

无法查找的设备上的 lseek() 行为是实现定义的。 与此类设备关联的文件偏移值未定义。

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:

The behavior of lseek() on devices which are incapable of seeking is implementation-defined. The value of the file offset associated with such a device is undefined.

拒绝两难 2024-07-21 08:16:15

我不确定我是否理解你的问题,但这里有一些可能有帮助的想法。

  • 偏移0有效; 这意味着您位于文件的开头,
  • 具体取决于您的平台,off_t 可能会限制为 32 位无符号。
  • 您是否打算寻求与您当前职位相关的职位?

——马库斯Q

I'm not sure I understand your question, but here are a few thoughts which might help.

  • Offset 0 is valid; it means you are at the beginning of the file
  • Depending on your platform, off_t may well be limited to 32 bits unsigned.
  • Are you intending to seek relative to your current position?

-- MarkusQ

给我一枪 2024-07-21 08:16:15

您可能希望将测试更改为:

if ( (pos = lseek(file, (off_t)i, SEEK_CUR)) != -1 ) {

您可能在某处达到 -1,但在这里测试为 0。

You might want to change the test to:

if ( (pos = lseek(file, (off_t)i, SEEK_CUR)) != -1 ) {

You are probably hitting a -1 somewhere, but you're testing for 0 here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文