Hi,
Is there a way to specify how many decimal points that fread reads from a double value in a binary file?
Thanks
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 ...
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
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
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.
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.
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
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.
alright, the portion code of reading the binary file is:
and the printing to the file code is this:Code:fread(&voltage,sizeof(double),1,ifp); //reading binary file into temporary variables fread(¤t,sizeof(double),1,ifp); fread(&diameter,sizeof(double),1,ifp);
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.Code:fprintf(volt, "%lf\n", voltage); //printing out the values into the corresponding files fprintf(amp, "%10.14lf\n", current);
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
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.
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.
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
> 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
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.
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
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.