目录

int fflush(FILE *stream)

描述 (Description)

C库函数int fflush(FILE *stream)刷新int fflush(FILE *stream)的输出缓冲区。

声明 (Declaration)

以下是fflush()函数的声明。

int fflush(FILE *stream)

参数 (Parameters)

  • stream - 这是指向FILE对象的指针,指定缓冲流。

返回值 (Return Value)

此函数在成功时返回零值。 如果发生错误,则返回EOF并设置错误指示符(即feof)。

例子 (Example)

以下示例显示了fflush()函数的用法。

#include <stdio.h>
#include <string.h>
int main () {
   char buff[1024];
   memset( buff, '\0', sizeof( buff ));
   fprintf(stdout, "Going to set full buffering on\n");
   setvbuf(stdout, buff, _IOFBF, 1024);
   fprintf(stdout, "This is iowiki.com\n");
   fprintf(stdout, "This output will go into buff\n");
   fflush( stdout );
   fprintf(stdout, "and this will appear when programm\n");
   fprintf(stdout, "will come after sleeping 5 seconds\n");
   sleep(5);
   return(0);
}

让我们编译并运行上面的程序,它将产生以下结果。 这里程序保持缓冲到输出到buff直到它面对fflush()第一次调用,之后它再次开始缓冲输出,最后睡眠5秒。 它在程序发出之前将剩余的输出发送到STDOUT。

Going to set full buffering on
This is iowiki.com
This output will go into buff
and this will appear when programm
will come after sleeping 5 seconds
↑回到顶部↑
WIKI教程 @2018