C Programming Exercise Example 97

100 Classic C Examples

Title:Input some characters from the keyboard and send them one by one to the disk until a # is entered.

Program analysis:None.

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. All rights reserved. // #include<stdio.h> #include<stdlib.h> int main() { FILE*fp=NULL; char filename[25]; char ch; printf("Enter the name of the file you want to save to:\n"); gets(filename); if((fp=fopen(filename,"w"))==NULL) { printf("error: cannot open file!\n"); exit(0); } printf("Now you can enter some characters to save, ending with #:\n"); getchar(); while((ch=getchar())!='#'){ fputc(ch,fp); } fclose(fp); system("pause"); return 0; }

The output of the above example is:

输入你要保存到的文件的名称:
test.txt
现在你可以输入你要保存的一些字符,以#结束:
www.example.com
#

100 Classic C Examples

other extensions