1 / 9

正文文件操作应用举例 : 例 1 :打印如下图形到屏幕上与正文文件 ABC.TXT 中。 1 3 5 7 9 11

正文文件操作应用举例 : 例 1 :打印如下图形到屏幕上与正文文件 ABC.TXT 中。 1 3 5 7 9 11 13 15 17 19 打印行数 n 由键盘输入。 #include "stdio.h" void main() { int n,i,j,k;FILE *fw; printf("Input n=");scanf("%d",&n); fw=fopen("abc.txt","w"); k=1;. for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++)

danil
Download Presentation

正文文件操作应用举例 : 例 1 :打印如下图形到屏幕上与正文文件 ABC.TXT 中。 1 3 5 7 9 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 正文文件操作应用举例: 例1:打印如下图形到屏幕上与正文文件ABC.TXT中。 1 3 5 7 9 11 13 15 17 19 打印行数n由键盘输入。 #include "stdio.h" void main() { int n,i,j,k;FILE *fw; printf("Input n=");scanf("%d",&n); fw=fopen("abc.txt","w"); k=1;

  2. for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) { printf("%5c",' '); fprintf(fw,"%5c",' '); } for(j=1;j<=i;j++) { printf("%5d",k); fprintf(fw,"%5d",k); k+=2; } printf("\n");fprintf(fw,"\n"); } fclose(fw); }

  3. 例2:用正文文件A1.TXT提供数据建立一个矩阵,然后输出转置矩阵到正文文件A2.TXT。例2:用正文文件A1.TXT提供数据建立一个矩阵,然后输出转置矩阵到正文文件A2.TXT。 #include "stdio.h" #define N 3 void main() { long a[N][N],b[N][N];int i,j;FILE *fw,*fr; fr=fopen("a1.txt","r"); if(fr==NULL) { printf("File a1.txt not found.\n");return; } fw=fopen("a2.txt","w"); for(i=0;i<N;i++) for(j=0;j<N;j++) { fscanf(fr, "%ld",&a[i][j]);b[j][i]=a[i][j]; }

  4. for(i=0;i<N;i++) {for(j=0;j<N;j++) fprintf(fw, "%6ld",b[i][j]); fprintf(fw, "\n"); } fclose(fr);fclose(fw); } type a1.txt 显示的文件内容(该文件可用TC集成环境或WINDOWS记事本等编辑器建立) 1 2 3 5 6 7 8 9 10 输出文件a2.txt的内容 1 5 8 2 6 9 3 7 10

  5. 例3:编程统计一个C源程序文件中行数. #include "stdio.h" void main() { FILE *fr;char ch;char fname[81];int n=0; printf("Input C source file name:");gets(fname); fr=fopen(fname, "r"); if(fr==NULL) { printf("file %s not found.\n",fname);return; } while(1) { ch=fgetc(fr);if(feof(fr)) break; if(ch==10) n++; } printf("n=%d\n",n);fclose(fr); }

  6. 例4:编程判断一个C源程序中的{}是否配对 算法:设一个计数变量c,初值为0,遇到左{则加1,遇到右}则减1。扫描文件过程中,c<0或判断结束后,c>0都说明{}没有配对. #include "stdio.h" void main() { FILE *fr;char ch;char fname[81];int c; printf("Input C source file name:");gets(fname); fr=fopen(fname, "r"); if(fr==NULL) { printf("file %s not found.\n",fname);return; } c=0;

  7. while(1) { ch=fgetc(fr);if(feof(fr)) break; if(ch=='{')c++; if(ch=='}')c--; if(c<0) break; } if(c!=0) printf("{ } error.\n"); else printf("{ } right.\n"); fclose(fr); }

  8. 例4 删除一个C源程序文件中的注释。 算法:用空格擦写/* .... */部分。设两个长整型变量pos1,pos2,表示注释开始和结束的位置。注意查找注释开始与结束的方法。 #include "stdio.h" void main() { FILE *fp;char fname[81],ch1,ch2;long pos1,pos2; printf("Input a C source filename:\n");gets(fname); fp=fopen(fname,"rb+"); if(!fp) { printf("%s file not found.\n",fname);return; } ch1=fgetc(fp); while(!feof(fp)) { ch2=fgetc(fp);if(feof(fp)) break; if(ch1=='/'&&ch2=='*') pos1=ftell(fp)-2;

  9. if(ch1=='*'&&ch2=='/') { pos2=ftell(fp)-1;fseek(fp,pos1,SEEK_SET); for(;pos1<=pos2;pos1++) fputc(' ',fp); fseek(fp,0L,1); /*为何需要要这句?*/ } ch1=ch2; } fclose(fp); } A B C D … /* X X X X */ pos1 pos2 ch1 ch2 ch1 ch2 注意:ch1,ch2每循环一次只推进一个字节,才能保证搜索的完整性,如上图所示。

More Related