when i merge the files they are left unsorted.... i am wondering how to sort my output file. thanx ... here is my code:
Code:
#include <stdio.h>
#include <limits.h>
#define READ_MODE "rb"
#define WRITE_MODE "wb"
int main (void)
{
FILE *fpM1;
FILE *fpM2;
FILE *fpOut;
int recM1;
int recM2;
char file1ID[] = "P13Mrg1.bin";
char file2ID[] = "P13Mrg2.bin";
char fileOutID[] = "P13Mrg3.bin";
int sentinel = INT_MAX;
int mergeCnt = 0;
printf("Begin File Merge:\n");
if (!(fpM1 = fopen (file1ID, READ_MODE)))
printf("\aError on %s\n", file1ID) ;
if (!(fpM2 = fopen (file2ID, READ_MODE)))
printf("\aError on %s\n", file2ID);
if (!(fpOut = fopen (fileOutID, WRITE_MODE)))
printf("\aError on %s\n", fileOutID);
fread (&recM1, sizeof(int),1,fpM1);
if (feof(fpM1))
recM1 = sentinel;
fread (&recM2, sizeof(int), 1, fpM2);
if (feof(fpM2))
recM2 = sentinel;
while (!feof(fpM1) || !feof(fpM2))
{
if (recM1 <= recM2)
{
fwrite (&recM1, sizeof(int), 1, fpOut);
mergeCnt++;
fread (&recM1,sizeof(int), 1, fpM1);
if (feof(fpM1))
recM1 = sentinel;
}
else
{
fwrite (&recM2, sizeof(int), 1, fpOut);
mergeCnt++;
fread (&recM2,sizeof(int), 1, fpM2);
if (feof(fpM2))
recM2 = sentinel;
}
}
fclose (fpM1);
fclose (fpM2);
fclose (fpOut);
printf("End File Merge. %d items merged.\n", mergeCnt);
return 0;
}