C语言-从键盘输入字符直到#结束,并将字符写入文件
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fp = NULL;
char filename[25];
char ch;
printf("input saved file name:\n");
gets(filename);//从键盘输入字符串,与scanf功能类似,其原型是char*gets(char *str)
if((fp = fopen(filename, "w"))==NULL){
printf("error: cannot open file!\n");
exit(0);
}
printf("now please input some char and stop util #\n");
ch = getchar();//从键盘读入一个字符
while(ch!='#'){
fputc(ch, fp);//就是把ch里的内容,写进fp指针指向的文件中,同时fp后移一个字节
ch = getchar();
}
fclose(fp);
system("pause");
return 0;
}
标签:fp,字符,ch,键盘输入,C语言,char,printf
From: https://www.cnblogs.com/bonne-chance/p/17769941.html