c++freopen函数_使用示例的C语言中的freopen()函数

c++freopen函数

C语言中的freopen()函数 (freopen() function in C)

Prototype:

原型:

    FILE* freopen(const char *str, const char *mode, FILE *stream);

Parameters:

参数:

    const char *str, const char *mode, FILE *stream

Return type: FILE*

返回类型: FILE *

Use of function:

使用功能:

The prototype of the function freopen() is:

函数freopen()的原型为:

    FILE* freopen(const char *str, const char *mode, FILE *stream);

The freopen() function opens the existing stream into another file. The end-of-file and error flag is cleared in this process. The file named as str with its operation mode, opens it into the file stream named as stream. The freopen() function acts similar to the fopen() function. In the following output we can see the work of the function.

freopen()函数将现有流打开到另一个文件中。 文件结束和错误标志在此过程中被清除。 以其操作模式命名为str的文件将其打开到名为stream的文件流中 。 freopen()函数的作用类似于fopen()函数。 在以下输出中,我们可以看到该函数的工作。

C中的freopen()示例 (freopen() example in C)

#include <stdio.h>
#include <stdlib.h>

int main(){
	//Initialize the file pointer
	FILE *f,*fp;
	//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 
		//except for last entry.Otherwise print the last line twice
		fputs("\n",f);

		//clear the stdin stream buffer
		//if we don't write this then after taking string 
		fflush(stdin);
	}
	//%[^\n] is waiting for the '\n' or white space
	//take the strings from the users
	scanf("%[^\n]",&ch);
	fputs(ch,f);

	//reopen the file for read operation
	fp=freopen("includehelp.txt","r",fp); 
	printf("File content is--\n");
	printf("\n...............print the strings..............\n\n");
	while(!feof(fp)){
		//takes the first 100 character in the character array 
		fgets(ch,100,fp);
		//and print the strings
		printf("%s",ch);
	}
	//close the files
	fclose(fp);
	fclose(f);
	
	return 0;
}

Output

输出量

freopen example in c

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

c++freopen函数

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值