#include<stdio.h>
#include<stdlib.h>
int main()
{
char ch;
FILE *fp;
**//判断文件是否成功打开**
//if((fp=fopen("demo.txt","w"))==NULL)
// {
//printf("Failure to open demo.txt!\n");
//exit(0);
// }
fp=fopen("demo.txt","w");
if(fp==NULL)
{
printf("Failure to open demo.txt!\n");
exit(0);
}
ch=getchar();
**//使用getchar()输入字符时,所有字符输入缓冲区
//直到键入回车换行符时将字符从该缓冲区中读出赋值**
while(ch!='\n')**//键入回车换行符,结束键盘输入和写入**
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);**//关闭文件**
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int ch;
if((fp=fopen("demo.txt","w"))==NULL)
{
printf("Failure to open demo.txt!\n");
exit(0);
}
ch=getchar();
while(ch!='\n')
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
return 0;
}