第9章文件9. 9.1.2 文件的模式 如 int 型数 10000 00100111 00010000 内存存储形式 00100111 00010000 二进制形式 00110001 00110000 00110000 00110000 00110000 ASCII 形式 文本文件特点 : 存储量大、速度慢、便于对字符操作 二进制文件特点 : 存储量小、速度快、便于存放中间结果 15. 9.2.1 文件的打开与关闭 文件打开模式 Mode 含 义 r 打开一个文本文件只读 w 打开一个文本文件只写 a 打开一个文本文件在尾部追加 rb 打开一个只读的二进制文件 wb 打开一个只写的二进制文件 ab 对二进制文件追加 r+ 打开一个可读 / 写的文本文件 w+ 创建一个新的可读 / 写的文本文件 a+ 打开一个可读 / 写的文本文件 rb+ 打开一个可读 / 写的二进制文件 wb+ 创建一个新的可读 / 写的二进制文件 ab 打开一个可读 / 写的二进制文件 Hinweis der Redaktion #include <string.h> #include <stdio.h> struct stu{ /* 定义结构体类型 */ char name[15]; char num[6]; float score[2]; } student; /* 说明结构体变量 */ int main(){ FILE *fp; int i; if ((fp=fopen(&quot;test.txt&quot;,&quot;w&quot;))==NULL) { /* 以文本只写方式打开文件 */ printf(&quot;cannot open file&quot;); exit(0); } printf(&quot;input data:\\n&quot;); for( i=0;i<2;i++){ scanf(&quot;%s %s %f %f&quot;,student.name,student.num, &student.score[0],&student.score[1]); fprintf(fp,&quot;%s %s %7.2f %7.2f\\n&quot;,student.name,student.num, student.score[0], student.score[1]) ; } fclose(fp); if ((fp=fopen(&quot;test.txt&quot;,&quot;r&quot;))==NULL){/* 以文本只读方式打开文件 */ printf(&quot;cannot open file&quot;); exit(0); } printf(&quot;output from file\\n&quot;); /* 从文件读入 */ while ( fscanf(fp,&quot;%s %s %f %f\\n&quot;,student.name,student.num, &student.score[0], &student.score[1]) != EOF ) printf(&quot;%s %s %7.2f %7.2f\\n&quot;,student.name,student.num, student.score[0],student.score[1]); /* 显示到屏幕 */ fclose(fp); return 0; } #include <string.h> #include <stdio.h> # define N 30 void save(); void open(); struct student{ char name[8]; int num; int age; float score; } stud[N]; int main( ){ int i; for (i=0;i<N;i++) scanf(&quot;%s%d%d%f&quot;,stud[i].name,&stud[i].num, &stud[i].age,&stud[i].score); save( ); printf(&quot;Press any key to show data from file\\n&quot;); getchar(); open(); return 0; } void save(){ FILE *fp; int i; if ((fp=fopen(&quot;score.dat&quot;,&quot;wb&quot;))==NULL){ printf(&quot;cannot open file\\n&quot;); return; } for (i=0;i<N;i++) if (fwrite(&stud[i],sizeof(struct student),1,fp)!=1) printf(&quot;file write error\\n&quot;); fclose(fp); } void open(){ FILE *fp; int i; if ((fp=fopen(&quot;score.dat&quot;,&quot;rb&quot;))==NULL) { printf(&quot;cannot open file\\n&quot;); return; } for (i=0;i<N;i++) if (fread(&stud[i],sizeof(struct student),1,fp)==1) printf(&quot;%-10s%8d%6d%12.2f\\n&quot;,stud[i].name,stud[i].num, stud[i].age,stud[i].score); else{ printf(&quot;file read error\\n&quot;); break; } fclose(fp); } #include <stdio.h> void fcopy(FILE *, FILE *); int main(int argc, char *argv[]){ FILE *inputfile,*outputfile; char *name = argv[0]; if (argc == 1) { /* 没有参数,从标准输入复制到标准输出 */ fcopy(stdin, stdout); return 0; } else if (argc == 2) { /* 有一个参数,从文件中复制到标准输出 */ if ((inputfile = fopen(argv[1], &quot;r&quot;)) == NULL){ printf(&quot;%s, can't open input file: %s\\n&quot;, name, argv[1]); exit(0); } else fcopy(inputfile, stdout); } else if (argc == 3) { /* 有两个参数,从文件中复制到指定的文件中 */ if ((inputfile = fopen(argv[1], &quot;r&quot;)) == NULL){ printf(&quot;%s, can't open input file: %s\\n&quot;, name, argv[1]); exit(0); } if ((outputfile = fopen(argv[2], &quot;w&quot;)) == NULL){ printf(&quot;%s, can't create output file: %s\\n&quot;, name, argv[2]); exit(0); } else fcopy(inputfile, outputfile); fclose(outputfile); } fclose(inputfile); return 0; } void fcopy(FILE *inputfile, FILE *outputfile){ int c; while ((c = fgetc(inputfile)) != EOF) fputc(c, outputfile); } #include <string.h> #include <stdio.h> int main(){ FILE *fptr1,*fptr2,*fptr3; /* 定义文件指针 */ char temp[15],temp1[15],temp2[15]; /* 打开文件 */ if ( (fptr1=fopen(&quot;addr.txt&quot;,&quot;r&quot;)) == NULL || (fptr2=fopen(&quot;tel.txt&quot;,&quot;r&quot;)) == NULL) || (fptr3=fopen(&quot;addrtel.txt&quot;,&quot;w&quot;)) == NULL) ){ printf(&quot;cannot open file&quot;); exit(0); } /* 读回的姓名字段长度大于 1 */ while(fscanf(fptr1,&quot;%s&quot;,temp1) != EOF){ fscanf(fptr1,&quot;%s&quot;,temp2); /* 读地址 */ fprintf(fptr3,&quot;%20s&quot;,temp1); /* 写入姓名到合并文件 */ fprintf(fptr3,&quot;%20s&quot;,temp2); /* 写入地址到合并文件 */ strcpy( temp, temp1 ) ; /* 保存姓名字段 */ /* 查找姓名相同的记录 */ do{ fscanf(fptr2,&quot;%s&quot;,temp1); fscanf(fptr2,&quot;%s&quot;,temp2); } while (strcmp(temp,temp1)!=0); rewind( fptr2 ); /* 将文件指针移到文件头,以备下次查找 */ fprintf(fptr3,&quot;%20s\\n&quot;,temp2); /* 将电话号码写入合并文件 */ } fclose( fptr1 ); /* 关闭文件 */ fclose( fptr2 ); fclose( fptr3 ); return 0; }