Hello there guys,
I need some help with my program. My program is supposed to collect and return the greater values from two binary files, which contain numbers, and amount of numbers are equal. Then write the values into the third binary file.
I have two files which contain: 12 34 and 11 35, so it is supposed to collect 12 35. So the problems is that the program only collects the only one number from the end, like only 35 and I have no clue how to fix this.
Any help would be appreciated, thanks.Code:#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *fp, *firstfile, *secondfile;
//char file_name[260];
int firstfilenum=0, secondfilenum=0, thirdfilenum=0;
/* File creation
int f;
printf("\nFile name: ");
scanf("%s", file_name);
fp = fopen(file_name, "wb");
printf("Enter information. To end press ctrl+z\n");
while(!feof(stdin)){
scanf("%d", &f);
if (!feof(stdin))
fwrite(&f, sizeof(int), 1, fp);
}
fclose(fp);
*/
firstfile = fopen("1.dat" , "r");
printf("Numbers on the first file: ");
while (!feof(firstfile))
{
fread(&firstfilenum,sizeof(int),1,firstfile);
printf("%d ", firstfilenum);
}
secondfile = fopen("2.dat" , "r");
printf("\nNumbers on the second file: ");
while(!feof(secondfile))
{
fread(&secondfilenum,sizeof(int),1,secondfile);
printf("%d ", secondfilenum);
}
fp = fopen("3.dat", "wb");
while(!feof(fp))
{
fscanf(firstfile,"%d",&firstfilenum);
fscanf(secondfile,"%d",&secondfilenum);
thirdfilenum = fmax(firstfilenum, secondfilenum);
{
fwrite(&thirdfilenum, sizeof(int), 1, fp); }
{
printf("\nSelected numbers to the third file: ");
{
rewind(fp);
fread(&thirdfilenum,sizeof(int),1,fp);
printf("%d\n\n", thirdfilenum);
}
fclose(firstfile);
fclose(secondfile);
fclose(fp);
system("pause");
return 0;
}
}
}