Hi,

Is there a way to specify how many decimal points that fread reads from a double value in a binary file?

Thanks

2. Originally Posted by firyace
Hi,

Is there a way to specify how many decimal points that fread reads from a double value in a binary file?

3. but then is there another way to read in a binary file and specifying the number of decimal points it reads for double?

4. 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. 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.

6. 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.

7. 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.

8. alright, the portion code of reading the binary file is:
Code:
```fread(&voltage,sizeof(double),1,ifp); //reading binary file into temporary variables
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.

9. It's surprising you're getting anything, since &#37;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

10. Originally Posted by MacGyver
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. add half of the value to round to. Then floor it.

 Whoops I should have read more of this topic before replying [/edit]

12. Originally Posted by Salem
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

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

Thanks!

13. > Well, can you please tell me what is the right way to read a double?
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```

14. 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

Originally Posted by Salem
> Well, can you please tell me what is the right way to read a double?
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```

15. Dude, we're not a manual page reading service.