为什么 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,SEEK_SET);

    f = fdopen(fd,"r");

    printf("%c\n",fgetc(f));
    printf("%d\n",lseek(fd,SEEK_CUR));
}

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

h
12

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

如果我重复第一个打印语句,它会按预期工作并打印 e 然后是 l

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

解决方法

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

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

如果您想获得考虑缓冲区中内容的文件位置,请改用 ftell()