一.fputc(ch,pc)
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{
FILE *fp;
char ch,filename[20];
printf("Please input the filename you want to write: ");
scanf("%s",filename);
if(!(fp=fopen(filename,"wt+")))
{
printf("Cannot open the file!\n");
exit(0);
}
printf("Please input the sentences you want to write :");
ch=getchar();
ch=getchar();
while(ch!=EOF)
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
return 0;
}
第二个getchar用来接收回车 防止回车被当成一个字符写入文件中
找到文件结果如下:
二.fgetc(fp)
while(ch!=EOF)
{
ch=fgetc(fp);
putchar(ch);
}
注意文件txt的末尾应该有 ^z,才能使文件读出结束