/************
文件操作
******************/
/*用函数fgets(),用键盘输入一串字符,添加到文本末尾,并打印出来*/
#include <stdio.h>
#include <stdilb.h>
#define N 80
int main()
{
FILE *fp;
char str[N];
if((fp = fopen("demo.txt","a")) == NULL)
{
printf("Failure to open demo!\n");
exit(0);
}
gets(str);
fputs(str,fp); //将字符串str写入tp所指的文件
fclose(fp);
if((fp = fopen("demo.txt","r")) == NULL)
{
printf("Failure to open demo!\n");
exit(0);
}
fgets(str,N,fp); //从fp所指的文件中读取字符串,最多N-1个字符
puts(str); //将字符串送到屏幕显示
fclose(fp);
return 0;
}
文件操作
******************/
/*用函数fgets(),用键盘输入一串字符,添加到文本末尾,并打印出来*/
#include <stdio.h>
#include <stdilb.h>
#define N 80
int main()
{
FILE *fp;
char str[N];
if((fp = fopen("demo.txt","a")) == NULL)
{
printf("Failure to open demo!\n");
exit(0);
}
gets(str);
fputs(str,fp); //将字符串str写入tp所指的文件
fclose(fp);
if((fp = fopen("demo.txt","r")) == NULL)
{
printf("Failure to open demo!\n");
exit(0);
}
fgets(str,N,fp); //从fp所指的文件中读取字符串,最多N-1个字符
puts(str); //将字符串送到屏幕显示
fclose(fp);
return 0;
}