从键盘输入字符后,写入到磁盘文件datafile1.txt中
#include<stdio.h>
void main()
{
FILE * fp1;
char str1;
if((fp1=fopen("C:\\datafile1.txt","w"))==NULL)
{
printf("file cannot be opened\n");
exit(1);
}
while((str1=fgetc(stdin))!='\n')
fputc(str1,fp1);
fclose(fp1);
}
读出磁盘文件datafile.txt中的内容,将它们显示在屏幕上
#include<stdio.h>
void main()
{
FILE * fp;
char str1;
if((fp=fopen("C:\\datafile.txt","r"))==NULL)
{
printf("file cannot be opened\n");
exit(1);
}
while((str1=fgetc(fp))!=EOF)
fputc(str1,stdout);
fclose(fp);
}