- 论坛徽章:
- 0
|
呵呵,我想出来了。原来问题出在这里。- /* ex7, ch13, cl ver. */
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 256
- int main(int argc, char *argv[])
- {
- FILE * f1, *f2, *fp1, *fp2;
- char buf1[MAX], buf2[MAX], ch, *buf; //这里的buf也不用初始化了,后面再说
- long cnt1 = 0, cnt2 = 0;
- if(argc != 3)
- {
- printf("Usage: %s file1 file2", argv[0]);
- exit(EXIT_FAILURE);
- }
- if((f1 = fopen(argv[1], "r")) == NULL)
- {
- fprintf(stderr, "Error opening %s", argv[1]);
- exit(EXIT_FAILURE);
- }
- if((f2 = fopen(argv[2], "r")) == NULL)
- {
- fprintf(stderr, "Error opening %s", argv[2]);
- exit(EXIT_FAILURE);
- }
- while((ch = getc(f1)) != EOF)
- cnt1++;
- while((ch = getc(f2)) != EOF)
- cnt2++;
- if(cnt1 >= cnt2)
- {
- fp1 = f1;
- fp2 = f2;
- }
- else
- {
- fp1 = f2;
- fp2 = f1;
- }
- rewind(f1);
- rewind(f2);
- while(fgets(buf1, MAX, fp1) != NULL)
- {
- buf = buf1; //buf经过一次循环之后所指向的是上一次打印的字符数组的最后一个元素,需要将其还原成新数组的首元素地址
- while(*++buf != '\n')
- putchar(*buf);
- printf(" ");
- while(fgets(buf2, MAX, fp2) != NULL)
- {
- puts(buf2);
- break;
- }
- }
- fclose(f1);
- fclose(f2);
- puts("\nEnd of Program.");
- return 0;
- }
复制代码 |
|