合并两个已经按递增排序的文件 #include <stdio.h> #include <stdlib.h> #define SIZE 1024 int main(int argc,char **argv) { if(argc != 4) { printf("Please try Use --help\n"); return -1; } FILE *fp1,*fp2,*fp3; int t1,t2; if((fp1 = fopen(argv[1],"r")) == NULL) { perror("open error"); return -1; } if((fp2 = fopen(argv[2],"r")) == NULL) { perror("open error"); return -1; } if((fp3 = fopen(argv[3],"w")) == NULL) { perror("open error"); return -1; } fread(&t1,sizeof(int),1,fp1); fread(&t2,sizeof(int),1,fp2); //printf("%d%d\n",t1,t2); /*两个文件都未读取完*/ while((!feof(fp1))&&(!feof(fp2))) { if(t1 < t2) { fwrite(&t1,sizeof(int),1,fp3); fread(&t1,sizeof(int),1,fp1); } else { fwrite(&t2,sizeof(int),1,fp3); fread(&t2,sizeof(int),1,fp2); } } /*文件2读取完成*/ while(!feof(fp1)) { fwrite(&t1,sizeof(int),1,fp3); fread(&t1,sizeof(int),1,fp1); } /*文件1读取完*/ while(!feof(fp2)) { fwrite(&t2,sizeof(int),1,fp3); fread(&t2,sizeof(int),1,fp2); } /*不要忘记关闭文件指针*/ fclose(fp1); fclose(fp2); fclose(fp3); return 0; }