Thread: Binary File -> End of File

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    Binary File -> End of File

    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!

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Yup, makes sense! Thanks...

    Added this:

    Code:
    fread(&x[i],sizeof(double),1,f);
    if(feof(f)) break;

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Go back and read it again, because seem to have ignored the title of the FAQ, and I suspect the contents as well. Try again, staring with the title:

    Why it's bad to use feof() to control a loop

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hint: check the return value of fread().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And you're also using the wrong format specifier to print a double. You might want to fix that after you're done rewriting your read loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. reading and writing to the end of a binary file
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2005, 04:03 PM
  5. Checking if I'm at end of file?
    By JOsephPataki in forum C Programming
    Replies: 5
    Last Post: 05-19-2003, 05:27 PM