#include "stdio.h"
void main()
{
char ch;
FILE *fp;
if ((fp=fopen("d:\\test.txt","wt+"))==NULL)
{
printf("\nError on open test.txt file!");
getchar();
}
printf("Input a string:\n");
ch=getchar();
while (ch!='\n')
{
fputc(ch,fp);
ch=getchar();
}
printf("Input ends. Now show the input strings:\n");
rewind(fp);
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
printf("\n");
if (fclose(fp)==0)
printf("File closed successfully\n");
else
printf("File closed unsuccessfully\n");
getchar();
}
rewind(FILE *fp)的作用是将文件内指针从文件尾部重新指向文件头。
文本文件的写入和读取-fgetc()和fputc()
最新推荐文章于 2022-12-16 00:04:04 发布