为什么fflush(input_filestream)不会像联机帮助页中所述丢弃Linux上的缓冲区? [英] Why fflush(input_filestream) does not discard buffer on linux as manpage describes?

查看:140
本文介绍了为什么fflush(input_filestream)不会像联机帮助页中所述丢弃Linux上的缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

man fflush:

对于与可搜索文件(例如,磁盘文件,但不包括管道或终端)相关联的输入流,fflush()丢弃已从基础文件获取但未被应用程序使用的所有缓冲数据. >

For input streams associated with seekable files (e.g., disk files, but not pipes or terminals), fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.

我已经阅读了一些有关fflush(stdin)fflush()的问题,根据标准,输入流是未定义的行为,没错.但是特别是对于Linux,我已经读到评论说它不是未定义的行为,而是扩展,是的,上面的联机帮助页说明了它如何在文件输入流上工作.

I've read some questions talking about fflush(stdin) and fflush() a input stream is undefined behaviour according to the standard, that's right. But for Linux specifically, I've read comments says that it's not a undefined behaviour but an extension, yes, the manpage above says how it would work on a file input stream.

那么,为什么fflush()输入文件流没有丢弃已从基础文件中提取但尚未被应用程序使用的缓冲数据.如联机帮助页所述?

So, why fflush() a input filestream does not discard buffered data that has been fetched from the underlying file, but has not been consumed by the application. as the manpage describes ?

在进行网络搜索之前,我完全相信该联机帮助页,现在我想知道它是否错误.

Before searching on the Web I fully believe the manpage now I want to know if it's wrong.

示例代码:

haha​​2.txt:123456,没有换行或空格.

haha2.txt: 123456, no line feed or whitespace.

#include <stdio.h>

int
main()
{
    FILE* fp = fopen("haha2.txt", "r");
    int q = getc(fp);
    fflush(fp);
    int j = getc(fp); // I expect the rest variables to be -1(EOF) starting from here
    int j2 = getc(fp);// But they can still read data as if fflush does not executed
    int j3 = getc(fp);
    int j4 = getc(fp);

    int j5 = getc(fp);
    int j6 = getc(fp);
    int j7 = getc(fp);
    int j8 = getc(fp); 
    printf("%c,%c,%c,%c,%c,%c,%c,%c\n", j,j2,j3,j4,j5,j6,j7,j8);
    return(0);
}

推荐答案

刷新文件缓冲区只会丢弃缓冲区中的数据.它不会影响文件的内容.随后的读取将继续读取文件,就像什么也没发生一样(除了必须先重新填充缓冲区).

Flushing the file buffer only discards the data from the buffer. It doesn't impact the file's contents. Subsequent reads will just keep reading the file as if nothing happened (except that the buffer first has to be filled again).

当然,这假设与此同时文件没有发生任何变化(例如,某些其他进程覆盖文件的一部分,将其截断等).

Of course, this assumes that nothing happened to the file in the meantime (eg. some other process overwriting part of the file, truncating it, etc.).

为了说明,如果您修改代码以在fflush之后包含sleep:

To illustrate, if you modify your code to include a sleep after the fflush :

#include <stdio.h>
#include <unistd.h>

int main() {
    FILE* fp = fopen("haha2.txt", "r");
    int q = getc(fp);
    fflush(fp);
    sleep(10);
    int j = getc(fp); // I expect the rest variables to be -1(EOF) starting from here
    printf("%d\n", j);
    return(0);
}

在运行此代码时,以及在另一个终端上,请在睡眠期间运行此命令(此命令将从文件中删除所有内容并将其大小设置为0):

When you run this code, and in another terminal, you run this command during the sleep (this command will remove all contents from the file and set its size to 0) :

truncate -s0 haha2.txt

然后代码将打印-1(因为已到达文件末尾).

Then the code will print -1 (because the end of the file has been reached).

如果再次尝试相同操作(确保首先将测试数据重新添加到文件中),但是这次没有fflush,则代码将在文件中打印第二个字符,因为文件的开头仍处于缓冲状态(即使实际文件不再包含任何内容).

If you try the same again (make sure to add your test data back into the file first), but this time without the fflush, the code will print the second character in the file, because the start of the file was still buffered (even though the actual file no longer contains anything).

这篇关于为什么fflush(input_filestream)不会像联机帮助页中所述丢弃Linux上的缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆