fgets和fputs示例_C语言中的fputs()函数(带示例)

fgets和fputs示例

C中的fputs()函数 (fputs() function in C)

Prototype:

原型:

    int fputs(const char *string,FILE *filename);

Parameters:

参数:

    const char *string,FILE *filename

Return type: int

返回类型: int

Use of function:

使用功能:

In the file handling, through the fputs() function we take the string from the user and store it to the input stream and increments the file pointer indicator for accepting next string input. The prototype of the function fputs() is: int fputs(const char *string,FILE *filename);

在文件处理中,通过fputs()函数,我们从用户处获取字符串并将其存储到输入流,并递增文件指针指示符以接受下一个字符串输入。 函数fputs()的原型是: int fputs(const char * string,FILE * filename);

It returns the negative value on success and return EOF for failure. Here string is the array of character and filename is the name of the file stream.

成功时返回负值,失败则返回EOF 。 这里string是字符数组, filename是文件流的名称。

C中的fputs()示例 (fputs() 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 
		//except for last entry.Otherwise print the last line twice
		fputs("\n",f);				
		//clear the stdin stream buffer
		fflush(stdin);
	}
	
	//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("\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

输出量

fputs example in c

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

fgets和fputs示例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值