算法代码:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fp;
char ch;
fp = fopen("data.txt","w");
printf("Input a string:");
scanf("%c",&ch);
while(ch!='\n') //存入一行字符的条件
{
fputc(ch,fp); //fputc是字符读写库函数,使用while函数使得能够连续输入成一行字符
scanf("%c",&ch);
}
fclose(fp);
fp=fopen("data.txt","r");
ch=fgetc(fp);
while(ch!=EOF) //EOF作为文件结束标志
{
printf("%c",ch);
ch=fgetc(fp);
}
printf("\n");
fclose(fp);
return 0;
}
运行结果: