fflush() 函数在 stdio.h 头文件中定义。它有助于更新流,这意味着它将任何输出流的内容刷新到相应的文件中。
int fflush(FILE *stream); #where stream should be a file pointer
fflush() 函数接受一个参数。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| stream | 指向 FILE 对象的指针,该对象指定一个缓冲流 | 必需 |
fflush() 函数成功时返回零,错误时返回 EOF,并设置错误指示器。
| 输入 | 返回值 |
|---|---|
| 如果成功 | 零 |
| 如果错误 | EOF |
#include <stdio.h>
int main()
{
char chr[50];
FILE *pnt;
pnt = fopen("mytestfile.txt", "r+");
if (pnt)
{
fputs("Library function examples", pnt);
fflush(chr); // flushes the buffer
fgets(chr, 20, pnt);
puts(chr); // It displays buffer data in output
fclose(pnt);
return 0;
}
return 1;
}
输出
Library function examples
#include <stdio.h>
#include <stdlib.h>
int main()
{
char chr[20];
int i;
for (i = 0; i<4; i++)
{
scanf("%[^\n]s", chr);
printf("%s\n", chr);
// used to clear the buffer
// and accept the next string
fflush(stdin);
}
return 0;
}
输出
C Programming C Programming C Programming C Programming