C Exercise Example 97
100 cases of classic C language
Title: Enter a few characters from the keyboard, put them one by one up to the disk until you enter a date #.
Program Analysis: None.
Source Code:
// Created by www.w3big.com on 15/11/9. // Copyright © 2015年 本教程. All rights reserved. // #include<stdio.h> #include<stdlib.h> int main() { FILE*fp=NULL; char filename[25]; char ch; printf("输入你要保存到的文件的名称:\n"); gets(filename); if((fp=fopen(filename,"w"))==NULL) { printf("error: cannot open file!\n"); exit(0); } printf("现在你可以输入你要保存的一些字符,以#结束:\n"); getchar(); while((ch=getchar())!='#'){ fputc(ch,fp); } fclose(fp); system("pause"); return 0; }
Run the above example output is:
输入你要保存到的文件的名称: test.txt 现在你可以输入你要保存的一些字符,以#结束: www.w3big.com #