/*从键盘输入一串字符到d:\123.txt文件中,然后输出到屏幕上。*/ #includemain() { FILE *fp; //文件指针 fp=fopen("d:\\123.txt","w"); //写入文本文件 if(fp==NULL) { printf("Can not open this file!\n"); //文件不存在 exit(0); } char ch; ch=getchar(); while(ch!='#') //循环输入直到"#"结束 { fputc(ch,fp); //输出一个字符到fp指定的文件 ch=getchar(); //读入一个字符到ch } fclose(fp); //文件关闭 fp=fopen("d:\\123.txt","r"); //读文本文件 if(fp==NULL) { printf("Can not open this file!\n"); //文件不存在 exit(0); } while(!feof(fp)) //如果不是文件尾,则继续。 { ch=fgetc(fp); //从文件读入一个字符到ch putchar(ch); //输出字符 } fclose(fp); //文件关闭 }
Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号