【入門】C程序設計(第三版)譚浩強—第十二至十四章

 第十二至十四章要點整理圖:

C程序設計(第三版)譚浩強—第十二至十四章

 

第十三章例題:

  1. 例題13.1 
  2. #include<stdio.h> 
  3. #include<stdlib.h> 
  4. void main() 
  5.     FILE *fp; 
  6.     char ch,filename[10]; 
  7.     scanf("%s",filename);   /*輸入文件名*/ 
  8.     if((fp=fopen(filename,"w"))==NULL)  /*爲輸出打開一個文件*/ 
  9.     { 
  10.         printf("cannot open file\n"); 
  11.         exit(0);    /*終止程序*/ 
  12.     } 
  13.     ch=getchar();   /*此語句用來接收在執行scanf語句時最後輸入的回車符*/ 
  14.     ch=getchar();   /*接收輸入的第一個字符*/ 
  15.     while(ch!='#'
  16.     { 
  17.         fputc(ch,fp);   /*把字符寫到磁盤文件上去*/ 
  18.         putchar(ch);    /*終端顯示*/ 
  19.         ch=getchar(); 
  20.     } 
  21.     putchar(10); 
  22.     fclose(fp); 
  23.  
  24. 例題13.2 
  25. #include<stdio.h> 
  26. #include<stdlib.h> 
  27. void main() 
  28.     FILE *in,*out
  29.     char ch,infile[15],outfile[15];     /*注意數組長度,在輸入文件名時*/ 
  30.     printf("Enter the infile name:\n"); 
  31.     scanf("%s",infile); 
  32.     printf("Enter the outfile name:\n"); 
  33.     scanf("%s",outfile); 
  34.     if((in=fopen(infile,"r"))==NULL
  35.     { 
  36.         printf("cannot open infile\n"); 
  37.         exit(0); 
  38.     } 
  39.     if((out=fopen(outfile,"w"))==NULL
  40.     { 
  41.         printf("cannot open outfile\n"); 
  42.         exit(0); 
  43.     } 
  44.     while(!(feof(in))) 
  45.     { 
  46.         fputc(fgetc(in),out); 
  47.     } 
  48.     fclose(out); 
  49.     fclose(in); 
  50.  
  51. #include<stdio.h> 
  52. #include<stdlib.h> 
  53. void main(int argc,char *argv[]) 
  54.     FILE *in,*out
  55.     char ch; 
  56.     if(argc!=3) 
  57.     { 
  58.         printf("You forgot to enter a filename\n"); 
  59.         exit(0); 
  60.     } 
  61.     if((in=fopen(argv[1],"r"))==NULL
  62.     { 
  63.         printf("cannot open infile\n"); 
  64.         exit(0); 
  65.     } 
  66.     if((out=fopen(argv[2],"w"))==NULL
  67.     { 
  68.         printf("cannot open outfile\n"); 
  69.         exit(0); 
  70.     } 
  71.     while(!feof(in)) 
  72.     { 
  73.         fputc(fgetc(in),out); 
  74.     } 
  75.     fclose(in); 
  76.     fclose(out); 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章