The binary file output has one extra line then the original line. Is the EOF detected correctly or there is a bug while writing the binary file?
Can someone pl look the code and reason..
Code:
#include <stdio.h>
#include<conio.h>
int main()
{
int i,j;
FILE *f,*fp;
double x[3];
f=fopen("fm.bin","wb");
fp = fopen("fmout.txt","r");
if (!f)
return 1;
if (!fp)
return 1;
while(!feof(fp))
{
for(i=0;i<=2;i++)
{
fscanf(fp," %lf ",&x[i]);
fwrite(&x[i],sizeof(double),1,f);
printf(" %lf ",x[i]);
}
printf("\n");
}
fclose(f);
fclose(fp);
printf("\n Reading Binary file...\n");
f = fopen("fm.bin","rb");
while(!feof(f))
{
printf("\n");
for(i=0;i<=2;i++)
{
fread(&x[i],sizeof(double),1,f);
printf(" %lf ",x[i]);
}
getch();
}
fclose(f);
}
Output:
Contents of fmout.txt:
Code:
0.981649 0.02342 0.1342
Contents printed on screen from Binary file
Code:
0.981649 0.02342 0.1342
0.981649 0.02342 0.1342
Thanks!