c语言中fflush函数z
C中的fflush()函数 (fflush() function in C)
Prototype:
原型:
int fflush(FILE *filename);
Parameters:
参数:
FILE *filename
Return type: 0 or EOF
返回类型: 0或EOF
Use of function:
使用功能:
When we are dealing with file handling, instead of handling the files we handle the streams. There are three types of streams stdin (standard input), stderr (standard error), stdout (standard output). fflush() function is used to flush the buffer after each iteration in the program. When we open a file for the write operation, a call to fflush() function helps to write on the file and also it clears the buffer from the stream. The prototype of the ffiush() function is: int fflush(FILE* filename);
当我们处理文件处理时,我们处理流而不是处理文件。 有三种类型的流的标准输入 (标准输入), 标准错误 (标准误差),由于输出 (标准输出)。 fflush()函数用于在程序中的每次迭代之后刷新缓冲区。 当我们打开文件进行写操作时,对fflush()函数的调用有助于在文件上进行写操作,并且还从流中清除缓冲区。 ffiush()函数的原型是: int fflush(FILE * filename);
A return value zero indicates that it is successful and a return value EOF implies that there is some error occurred.
返回值零表示成功,而返回值EOF表示发生了一些错误。
C中的fflush()示例 (fflush() example in C)
#include <stdio.h>
#include <stdlib.h>
int main()
{
//Initialize the file pointer
FILE *f;
//Take a array of characters
char ch[100];
//Create the file for write operation
f=fopen("includehelp.txt","w");
printf("Enter five strings\n");
for(int i=0;i<4;i++){
//take the strings from the users
scanf("%[^\n]",&ch);
//write back to the file
fputs(ch,f);
//every time take a new line for the new entry string
fputs("\n",f);
//except for last entry.Otherwise print the last line twice
//clear the stdin stream buffer
//fflush(stdin);
//if we don't write this then after taking string
//%[^\n] is waiting for the '\n' or white space
}
//take the strings from the users
scanf("%[^\n]",&ch);
fputs(ch,f);
//close the file after write operation is over
fclose(f);
//open a file
f=fopen("includehelp.txt","r");
printf("File content is--\n");
printf("\n...............print the strings..............\n");
while(!feof(f)){
//takes the first 100 character in the character array
fgets(ch,100,f);
//and print the strings
printf("%s",ch);
}
//close the file
fclose(f);
return 0;
}
Output
输出量
If we don't use the fflush() function here. Then the output will be...
如果我们在这里不使用fflush()函数。 然后输出将是...
翻译自: https://www.includehelp.com/c-programs/fflush-function-in-c-language-with-example.aspx
c语言中fflush函数z