读写文件中的字符串

//文件中读取字符串:调用函数fgets() ;
//charfgets(chars,int n,FILE*fp);

#include<stdio.h>
#include<stdlib.h>
#define N 80
int main()
{
	FILE *fp;
	char str[N];
	if((fp=fopen("demo.txt","a"))==NULL)          //添加方式打开文件 
	{ 
	printf("Failure to open demo.txt!\n");
	exit(0);
	}
	gets(str) ;
	fputs(str,fp);
	
	fclose(fp);
	if((fp=fopen("demo.bin","r"))==NULL)
	{
	printf("Failure to open demo.txt!\n"); 
	exit(0);
	 }
	 fgets(str,N,fp);
	 puts(str);
	 fclose(fp);
	 return 0; 
}	

在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/CSDN447447LJH/article/details/91479284