I have 2 txt files that I have converted to binary files. I need to take these two binary files merge them into one. The attempt I have made using a while loop created an endless loop. A fellow programer pointed out the error but not that I know what the problem is I have been unable to figure out a method that will successfully merge the two binary files. Here is the segment of code.
Code:
void mergeBinFiles(FILE* binFile1, FILE* binFile2, FILE** binOutFile)
{
//Local Declarations
INV_REC itemM1;
INV_REC itemM2;
INV_REC sentinel = {9999};
int mergeCnt = 0;
//int fp1;
//int fp2;
//Statements
printf("\nBegin File Merge:\n");
fread(&itemM1, sizeof(INV_REC), 1, binFile1);
if(feof(binFile1))
itemM1 = sentinel;
fread(&itemM2, sizeof (INV_REC), 1, binFile2);
if(feof(binFile2))
itemM2 = sentinel;
while((fgets(&itemM1, sizeof(INV_REC), binFile1) != NULL) ||
(fgets(&itemM2, sizeof(INV_REC), binFile2) != NULL))
{
if(strcmp(itemM1.partNo, itemM2.partNo) >= 0)
{
fwrite(&itemM2, sizeof(INV_REC), 1, *binOutFile);
mergeCnt++;
fread(&itemM2, sizeof(INV_REC), 1, binFile2);
if(feof(binFile2))
itemM2 = sentinel;
}//if
else
{
fwrite(&itemM1, sizeof(INV_REC), 1, *binOutFile);
mergeCnt++;
fread(&itemM1, sizeof(INV_REC), 1, binFile1);
if(feof(binFile1))
itemM1 = sentinel;
}//else
}//while
printf("\nFiles Merged. %d items merged.\n", mergeCnt);
system("Pause");
return;
}//mergeBinFiles
I now that the while statement is the problem. This is the original while statement:
Code:
while(!foef(binfile1) || !feof(binFile2))
I learned that this doesn't work because it tests for the end of file indicator, not the stream itself. So my question is how can I correct the problem? I tried the above and I have tried this:
Code:
while(binFile1 != NULL || binFile2 != NULL)
None of my solutions have corrected the problem and I am in need of some help. could someone help me please.