Problems with reading the end of a binary file

This is a discussion on Problems with reading the end of a binary file within the C Programming forums, part of the General Programming Boards category; Hi, i was given a binary file that i know the format of, which has three double values per line ...

  1. #1
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56

    Problems with reading the end of a binary file

    Hi,

    i was given a binary file that i know the format of, which has three double values per line like this:

    Code:
    <double>   <double>    <double>
    <double>   <double>    <double>
    <double>   <double>    <double>
          .                    .                      .                      
          .                    .                      .
          .                    .                      .
    The three columns are voltage, current and diameter correspondingly.
    What I have to do is to create 6 files , 2 each for voltage, current and diameter which is for capturing alternating rows.

    Here is my code so far:
    Code:
    while(1)
        {
          fread(&voltage,sizeof(double),1,ifp); //reading binary file into temporary variables
          fread(&current,sizeof(double),1,ifp);
          fread(&diameter,sizeof(double),1,ifp);
          
          
          
          if (diameter < 0.00000009)
    	{
    	  
    	  //vertexnum = i;
    	  //for(i=0; i<vertexnum; i++)
    	  // {
    	  //printf("%lf %10.29f %10.14f\n", out[i].voltage, out[i].current, out[i].diameter); 
    	  //}
    	  test = 1;
    	  break;
    }
        
          
          fprintf(volt1, "%lf\n", voltage);//printing out the values into the corresponding files
          fprintf(amp1, "%10.29f\n", current);
          fprintf(diam1, "%10.14f\n", diameter);
    
          if(i ==39)
    	{
    	  fprintf(volt1, "%lf\n", voltage);//printing out the values into the corresponding files
    	  fprintf(amp1, "%10.29f\n", current);
    	  fprintf(diam1, "%10.14f\n", diameter);
    	  
    	}
    
          fread(&voltage,sizeof(double),1,ifp); //reading binary file into temporary variables
          fread(&current,sizeof(double),1,ifp);
          fread(&diameter,sizeof(double),1,ifp);
    
          fprintf(volt2, "%lf\n", voltage);//printing out the values into the corresponding files
    
          fprintf(amp2, "%10.29f\n", current);
          fprintf(diam2, "%10.14f\n", diameter);
    
          if(i ==39)
    	{
         fprintf(volt2, "%lf\n", voltage);//printing out the values into the corresponding files
         fprintf(amp2, "%10.29f\n", current);
         fprintf(diam2, "%10.14f\n", diameter);
         
    	}
          
          i++;
          //printf("%lf %10.29f %10.14f\n", out[i].voltage, out[i].current, out[i].diameter); //print to screen for verification
    
        }
    where ifp is the file pointer to the file being read, whereas volt1, volt2, amp1, amp2, diam1 and diam 2 are the file being written on. on i =39, a new point is being written to the file. I am not given the number of lines in the binary file, and I have attempted to break the loop by establishing an if loop that said if diameter < 0.00000009, the loop would break. This was solely based on my hypothesis that when its the end of file, the diameter is zero (i know it wouldn't, but its a form of a desparate move). Apparently it will loop on the last line of the data, but since some of the data repeats too so I can't based on it to end the loop either.

    So how can I end the loop when it reaches to the end of the binary file?

    Thanks
    Firyace
    Undergraduate Research
    Electrical and Biomedical Engineering Department
    University of Calgary

    My Comp:
    |Core 2 Duo 6420 4mb cache| Corsair 2*1Gb memory pc5400|
    |500Gb and 80Gb Sata2| HIS 1950pro Turbo OC 256mb ViVo|
    |X-Cube2 red micro atx case| 3in1 Tiger Game port|
    |ASUS P5B-LD2 Rev2.0-VM| WindowsXP Pro SP2| Fedora 8|
    |Windows XP Pro 64|

    My Store
    Real estate 43

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > So how can I end the loop when it reaches to the end of the binary file?
    By looking at the return result of fread(), which you're ignoring at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    nevermind, I fixed it with feof function.
    Firyace
    Undergraduate Research
    Electrical and Biomedical Engineering Department
    University of Calgary

    My Comp:
    |Core 2 Duo 6420 4mb cache| Corsair 2*1Gb memory pc5400|
    |500Gb and 80Gb Sata2| HIS 1950pro Turbo OC 256mb ViVo|
    |X-Cube2 red micro atx case| 3in1 Tiger Game port|
    |ASUS P5B-LD2 Rev2.0-VM| WindowsXP Pro SP2| Fedora 8|
    |Windows XP Pro 64|

    My Store
    Real estate 43

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by firyace View Post
    nevermind, I fixed it with feof function.
    feof() is almost always a bad choice. You should check the return value of fread() to see when you reach the end of file.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > nevermind, I fixed it with feof function.
    Did you read the FAQ?
    Did you use feof() as the while condition?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Problems reading a binary file.
    By earth_angel in forum C++ Programming
    Replies: 2
    Last Post: 07-08-2005, 10:32 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21