c语言中fflush函数z_带有示例的C语言fflush()函数

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

输出量

fflush example in c 1

If we don't use the fflush() function here. Then the output will be...

如果我们在这里不使用fflush()函数。 然后输出将是...

fflush example in c 2

翻译自: https://www.includehelp.com/c-programs/fflush-function-in-c-language-with-example.aspx

c语言中fflush函数z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值