为什么fgetc将文件偏移量放在文件末尾?

我有一个简单的测试程序,用于fgetc()从文件流中读取字符并lseek()读取文件偏移量。它看起来像这样:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() {
    char buf[] = "hello world";
    FILE *f;
    int fd;

    fd = open("test.txt", O_RDWR | O_CREAT | O_TRUNC, 0600);
    write(fd, buf, sizeof(buf));
    lseek(fd, 0, SEEK_SET);

    f = fdopen(fd, "r");

    printf("%cn", fgetc(f));
    printf("%dn", lseek(fd, 0, SEEK_CUR));
}

当我运行它时,我得到以下输出:

h
12

的返回值fgetc(f)h,对我来说很有意义。但是为什么要将文件偏移量重新定位到文件末尾呢?为什么不lseek(fd, 0, SEEK_CUR)给我1?

如果我重复第一个打印语句,它会按预期工作并打印ethen anl等。

我在man页面中没有看到任何提及这种奇怪行为的内容。

回答

像 stdio 函数一样fgetc被缓冲。他们将read()一个大块放入缓冲区,然后在连续调用时从缓冲区返回字符。

由于默认缓冲区大小超过 12(通常很多 KB),因此第一次fgetc()尝试填充其缓冲区,这意味着读取整个文件。因此lseek返回文件末尾的位置。

如果要获取考虑缓冲区中内容的文件位置,请ftell()改用。


以上是为什么fgetc将文件偏移量放在文件末尾?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>