第十二至十四章要点整理图:
第十三章例题:
- 例题13.1
- #include<stdio.h>
- #include<stdlib.h>
- void main()
- {
- FILE *fp;
- char ch,filename[10];
- scanf("%s",filename); /*输入文件名*/
- if((fp=fopen(filename,"w"))==NULL) /*为输出打开一个文件*/
- {
- printf("cannot open file\n");
- exit(0); /*终止程序*/
- }
- ch=getchar(); /*此语句用来接收在执行scanf语句时最后输入的回车符*/
- ch=getchar(); /*接收输入的第一个字符*/
- while(ch!='#')
- {
- fputc(ch,fp); /*把字符写到磁盘文件上去*/
- putchar(ch); /*终端显示*/
- ch=getchar();
- }
- putchar(10);
- fclose(fp);
- }
- 例题13.2
- #include<stdio.h>
- #include<stdlib.h>
- void main()
- {
- FILE *in,*out;
- char ch,infile[15],outfile[15]; /*注意数组长度,在输入文件名时*/
- printf("Enter the infile name:\n");
- scanf("%s",infile);
- printf("Enter the outfile name:\n");
- scanf("%s",outfile);
- if((in=fopen(infile,"r"))==NULL)
- {
- printf("cannot open infile\n");
- exit(0);
- }
- if((out=fopen(outfile,"w"))==NULL)
- {
- printf("cannot open outfile\n");
- exit(0);
- }
- while(!(feof(in)))
- {
- fputc(fgetc(in),out);
- }
- fclose(out);
- fclose(in);
- }
- #include<stdio.h>
- #include<stdlib.h>
- void main(int argc,char *argv[])
- {
- FILE *in,*out;
- char ch;
- if(argc!=3)
- {
- printf("You forgot to enter a filename\n");
- exit(0);
- }
- if((in=fopen(argv[1],"r"))==NULL)
- {
- printf("cannot open infile\n");
- exit(0);
- }
- if((out=fopen(argv[2],"w"))==NULL)
- {
- printf("cannot open outfile\n");
- exit(0);
- }
- while(!feof(in))
- {
- fputc(fgetc(in),out);
- }
- fclose(in);
- fclose(out);
- }
转载于:https://blog.51cto.com/huaxia283611/1065135