- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h> // for exit()
#define MAX 256
void display_two_file(FILE *,FILE *);
int main(int argc,char *argv[])
{
FILE *fp1,*fp2;
if(argc==3)
{
if((fp1=fopen(argv[1],"r"))==NULL)
{
fprintf(stderr,"Can't open file %s\n",argv[1]);
exit(1);
}
if((fp2=fopen(argv[2],"r"))==NULL)
{
fprintf(stderr,"Can't open file %s\n",argv[2]);
exit(2);
}
display_two_file(fp1,fp2);
fclose(fp1);
fclose(fp2);
}
else
fprintf(stderr,"Usage: %s file file\n",argv[0]);
return 0;
}
void display_two_file(FILE *fp1,FILE *fp2)
{
char line[MAX];
while(! feof(fp1) && ! feof(fp2))
{
fgets(line,MAX,fp1);
fputs(line,stdout);
fgets(line,MAX,fp2);
fputs(line,stdout);
}
while(! feof(fp1))
{
fgets(line,MAX,fp1);
fputs(line,stdout);
}
while(! feof(fp2))
{
fgets(line,MAX,fp2);
fputs(line,stdout);
}
}
程序接收两个参数,交替打印文件的内容
a文件的内容是
a b
c
b文件的内容是
1 2
3
4 5
6
运行后的结果是
a b
1 2
c
3
3
4 5
6
6
好像每个文件的最后一行都打印了两次,为什么啊?求指点,C新手 |
|