fread problems

This is a discussion on fread problems within the C Programming forums, part of the General Programming Boards category; Hi, Is there a way to specify how many decimal points that fread reads from a double value in a ...

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

    fread problems

    Hi,

    Is there a way to specify how many decimal points that fread reads from a double value in a 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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by firyace View Post
    Hi,

    Is there a way to specify how many decimal points that fread reads from a double value in a binary file?
    No, since fread() is not a formatter, it simply reads bytes.

  3. #3
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    but then is there another way to read in a binary file and specifying the number of decimal points it reads for double?
    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
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Your question makes little sense. Think about it for a second.

    A double, in binary, is a fixed-width binary number that represents the floating point number in question. When you write it to the file, you have to write all 64-bits (or whatever the size is). When you read it, you can't ignore any bits. You have to read all of the bits that make up the double.

    Now if you have a double in memory, then you can print it however you want (with less precision perhaps). You can modify it and round it up or down, or do any other number of things to it.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you store the number in a text file with fprintf(), you can choose the number of digits to print. Or you can print a specific number of digits after you've read the number from the file, as MacGyver said. You can't store a specific number of digits in a binary file.

    Of course, you could round the number before storing it, but that would probably be slightly inaccurate, and when you read it you'd get something like 12.340001.
    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
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    well, the problem right now is that i want MORE precision (12-14 decimal places instead of the six decimal places), so when I printf to the screen and speciallize to 14 decimal places, it wouldn't just show 6 decimal places and the rest are then zero. And that is what is happening right now, i was told that one of the variables within the binary file contains 12-14 decimal places!

    Right now, this is what happens:
    The binary file has a table with each row containing 3 double (or so it is claimed).
    However, one of the double value contains 14 decimal places because its a small number (think 0.0000000000123). Right now, if I printf on the screen, no matter how many decimal places yiou set for printing, it only contains 0s since the double cannot read it.
    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

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Show us some actual code for reading the file and printing the result.

    There are 15 decimal digits of precision in a double, but you sometimes have to experiment with the printf() format string to get exactly what you want.
    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.

  8. #8
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    alright, the portion code of reading the binary file is:
    Code:
    fread(&voltage,sizeof(double),1,ifp); //reading binary file into temporary variables
          fread(&current,sizeof(double),1,ifp);
          fread(&diameter,sizeof(double),1,ifp);
    and the printing to the file code is this:

    Code:
    fprintf(volt, "%lf\n", voltage); //printing out the values into the corresponding files
          fprintf(amp, "%10.14lf\n", current);
    where volt and amp are file pointers. However, it doesn't seem %10.14 works in producing the values, as it is still 0 after the 6th value.
    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

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    It's surprising you're getting anything, since %lf is the WRONG format for printing doubles.
    You use %lf for scanf into a double, but this is printf (the symmetry of conversion operators is not universal).

    When you're using gcc to compile, use these flags
    -W -Wall
    and if you're not doing anything special with the OS
    -ansi -pedantic

    Oh, and add some error checking to your fread calls as well.
    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.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    51
    Quote Originally Posted by MacGyver View Post
    You can modify it and round it up or down, or do any other number of things to it.
    Just a little bit off topic, but, how do you round it in C?

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    add half of the value to round to. Then floor it.

    [edit] Whoops I should have read more of this topic before replying [/edit]
    Last edited by mike_g; 06-13-2007 at 11:03 AM.

  12. #12
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    Quote Originally Posted by Salem View Post
    It's surprising you're getting anything, since %lf is the WRONG format for printing doubles.
    You use %lf for scanf into a double, but this is printf (the symmetry of conversion operators is not universal).

    When you're using gcc to compile, use these flags
    -W -Wall
    and if you're not doing anything special with the OS
    -ansi -pedantic

    Oh, and add some error checking to your fread calls as well.


    Well, can you please tell me what is the right way to read a double?

    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

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > Well, can you please tell me what is the right way to read a double?
    I said printing, not reading.
    Code:
    #include<stdio.h>       
    #include<stdlib.h>
    
    int main()
    {
        double d;
        printf("&#37;lf",d);
        return 0;
    }
    
    $ gcc -W -Wall -ansi -pedantic foo.c
    foo.c: In function `main':
    foo.c:7: warning: ISO C90 does not support the `%lf' printf format
    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.

  14. #14
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    okay then, can you please tell me how to printf double, I have never encounter this problem using %lf until i need to printf more decimal places then I am suppose to.

    Thanks

    Quote Originally Posted by Salem View Post
    > Well, can you please tell me what is the right way to read a double?
    I said printing, not reading.
    Code:
    #include<stdio.h>       
    #include<stdlib.h>
    
    int main()
    {
        double d;
        printf("%lf",d);
        return 0;
    }
    
    $ gcc -W -Wall -ansi -pedantic foo.c
    foo.c: In function `main':
    foo.c:7: warning: ISO C90 does not support the `%lf' printf format
    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

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Dude, we're not a manual page reading service.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. Reading from binary file; fread problems?
    By pmgeahan in forum C Programming
    Replies: 3
    Last Post: 01-15-2009, 04:07 PM
  3. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  4. fread problems
    By Happy_Reaper in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 09:27 AM
  5. fread item_size limit
    By nvoigt in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2005, 08:08 AM

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