p376.7
A方案
1 #include2 #include 3 #define LEN 40 4 void rank(FILE *, FILE *); 5 int main(void) 6 { 7 FILE *fp, *fc; 8 int ch, bp; 9 char file1[LEN], file2[LEN];10 11 puts("enter file1 name");12 if((fp=fopen(gets(file1), "r"))==NULL){13 fputs("can't open", stdout);14 exit(1);15 }16 17 puts("enter file2 name");18 if((fc=fopen(gets(file2), "r"))==NULL){19 fputs("can't open", stdout);20 exit(2);21 }22 23 rank(fp, fc);24 if((fclose(fp)!=0)||(fclose(fc)!=0))25 puts("error in closing files");26 27 system("pause");28 return 0;29 }30 31 void rank(FILE *fp, FILE *fc)32 {33 int ch, bp;34 while(1){35 while(((ch=getc(fp))!='\n')&&ch!=EOF)36 putc(ch, stdout);37 if(ch=='\n')38 putc(ch, stdout);39 while(((bp=getc(fc))!='\n')&&bp!=EOF)40 putc(bp, stdout);41 if(ch=='\n')42 putc(bp, stdout);43 if((ch==EOF)&&(bp==EOF))44 break;45 }46 }
B方案
1 #include2 #include 3 #define LEN 40 4 void rank(FILE *, FILE *); 5 int main(void) 6 { 7 FILE *fp, *fc; 8 int ch, bp; 9 char file1[LEN], file2[LEN];10 11 puts("enter file1 name");12 if((fp=fopen(gets(file1), "r"))==NULL){13 fputs("can't open", stdout);14 exit(1);15 }16 17 puts("enter file2 name");18 if((fc=fopen(gets(file2), "r"))==NULL){19 fputs("can't open", stdout);20 exit(2);21 }22 23 rank(fp, fc);24 if((fclose(fp)!=0)||(fclose(fc)!=0))25 puts("error in closing files");26 27 system("pause");28 return 0;29 }30 31 void rank(FILE *fp, FILE *fc)32 {33 int ch, bp;34 while(1){35 while(((ch=getc(fp))!='\n')&&ch!=EOF)36 putc(ch, stdout);37 while(((bp=getc(fc))!='\n')&&bp!=EOF)38 putc(bp, stdout);39 if((bp=='\n')||(ch=='\n'))40 printf("\n");41 if((ch==EOF)&&(bp==EOF))42 break;43 }44 }
p376.8
1 #include2 #include 3 #define LEN 40 4 int main(int argc, char *argv[]) 5 { 6 FILE *fp, *fc, *fd[LEN]; 7 char name1[LEN], name2[LEN]; 8 int ch, bp, count[2]={ 0}, i, num[LEN]={ 0}; 9 if(argc==2){10 puts("enter file1 name");11 if((fp=fopen(gets(name1), "r"))==NULL)12 fputs("can't open", stderr);13 puts("enter file1 name");14 if((fc=fopen(gets(name2), "r"))==NULL)15 fputs("can't open", stderr);16 17 while((ch=getc(fp))!=EOF)18 if(ch==argv[1][0])19 count[0]++;20 while((bp=getc(fc))!=EOF)21 if(bp==argv[1][0])22 count[1]++;23 24 printf("%s has %d %c\n", name1, count[0], *argv[1]);25 printf("%s has %d %c\n", name2, count[1], *argv[1]);26 }27 28 for(i=2; i
p376.10
1 #include2 #include 3 #define LEN 40 4 int main(void) 5 { 6 FILE *fp; 7 char name[LEN]; 8 long offset; 9 int ch;10 11 puts("enter the file name");12 if((fp=fopen(gets(name), "r"))==NULL){13 fprintf(stderr, "can't open\n");14 exit(1);15 }16 puts("enter an offset of file(q to quit)");17 while(scanf("%ld", &offset)==1){18 fseek(fp, offset, SEEK_SET);19 while(((ch=getc(fp))!='\n')&&ch!=EOF&&ch!='\r')20 fprintf(stdout, "%c", ch);21 if(ch==EOF)22 exit;23 printf("\n");24 puts("enter an offset of file(q to quit)");25 }26 27 fclose(fp);28 system("pause");29 return 0;30 }