/* ******************************************************* * Author: Nikitas N. Karanikolas, Assistant Professor * * Creation Date: February 21, 2009 * ******************************************************* */ #include #include "types.h" main (int argc, char *argv[]) { personnel_rec e1, e2; int eof1, eof2; FILE *fp1, *fp2, *fp3; if (argc<4) { printf("You should provide:\n1. the name of the existing master file"); printf("\n2. the name of the file with the new records\n3. the new master file."); exit(1); } fp1=fopen(argv[1],"rb"); if (fp1==NULL) { printf("Can not open [%s] for reading.\n",argv[1]); exit(2); } fp2=fopen(argv[2],"rb"); if (fp2==NULL) { printf("Can not open [%s] for reading.\n",argv[2]); fclose(fp1); exit(3); } fp3=fopen(argv[3],"wb"); if (fp3==NULL) { printf("Can not open [%s] for writing.\n",argv[3]); fclose(fp1); fclose(fp2); exit(4); } eof1=fread(&e1,sizeof(personnel_rec),1,fp1)<1; eof2=fread(&e2,sizeof(personnel_rec),1,fp2)<1; while ((!eof1) && (!eof2)) if (strcmp(e1.key, e2.key)<0) { fwrite(&e1,sizeof(personnel_rec),1,fp3); eof1=fread(&e1,sizeof(personnel_rec),1,fp1)<1; } else if (strcmp(e1.key, e2.key)>0) { fwrite(&e2,sizeof(personnel_rec),1,fp3); eof2=fread(&e2,sizeof(personnel_rec),1,fp2)<1; } else /* strcmp(e1.key, e2.key)==0 */ { /* abnormal case, in case that the new master file should have unique keys */ printf("Already exist a record with key [%s] in the existing master file [%s]. ", e2.key, argv[1]); printf("The record of the file with the new records [%s] is discarded.\n", argv[2]); eof2=fread(&e2,sizeof(personnel_rec),1,fp2)<1; } while (!eof1) { fwrite(&e1,sizeof(personnel_rec),1,fp3); eof1=fread(&e1,sizeof(personnel_rec),1,fp1)<1; } while (!eof2) { fwrite(&e2,sizeof(personnel_rec),1,fp3); eof2=fread(&e2,sizeof(personnel_rec),1,fp2)<1; } fclose(fp1); fclose(fp2); fclose(fp3); }