Reading or writing a double into a bin file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 21
Reputation: fesago90 is an unknown quantity at this point 
Solved Threads: 1
fesago90's Avatar
fesago90 fesago90 is offline Offline
Newbie Poster

Reading or writing a double into a bin file

 
0
  #1
Jan 24th, 2007
Aright, now all of you might be tired of my sometimes info-lacking posts about binary files.
... but I have another quick question:
How do I write and read a double in a binary file?
Code as simple as the following does not work (which is the usual code for writing/reading an integer, string, etc. to a binary file)

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. double pi = 3.14;
  9. double read;
  10. ofstream binf1("bin.txt");
  11. binf1.close();
  12. fstream binf("bin.txt", ios::binary|ios::in|ios::out);
  13. binf.write(reinterpret_cast<char*>(&binf), sizeof(double));
  14. binf.read(reinterpret_cast<char*>(&read), sizeof(double));
  15. cout<< "\n" << read << "\n";
  16. system("PAUSE");
  17. return EXIT_SUCCESS;
  18. }

Thanks
Last edited by fesago90; Jan 24th, 2007 at 11:11 pm.
http://www.ulteo.com
Ulteo - Taste a bit of freedom
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Reading or writing a double into a bin file

 
0
  #2
Jan 25th, 2007
  1. binf.write(reinterpret_cast<char*>(&binf), sizeof(double));
Why are you writing to binf the address of itself? I don't know how much good that's going to do you...

Which is probably where your problem lies.

Also, you don't have to write double as a string to your binary file. If you wanted to, you could directly write the value to the file.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Reading or writing a double into a bin file

 
0
  #3
Jan 25th, 2007
Originally Posted by fesago90 View Post
Aright, now all of you might be tired of my sometimes info-lacking posts about binary files.
... but I have another quick question:
How do I write and read a double in a binary file?
Code as simple as the following does not work (which is the usual code for writing/reading an integer, string, etc. to a binary file)

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. double pi = 3.14;
  9. double read;
  10. ofstream binf1("bin.txt");
  11. binf1.close();
  12. fstream binf("bin.txt", ios::binary|ios::in|ios::out);
  13. binf.write(reinterpret_cast<char*>(&binf), sizeof(double));
  14. binf.read(reinterpret_cast<char*>(&read), sizeof(double));
  15. cout<< "\n" << read << "\n";
  16. system("PAUSE");
  17. return EXIT_SUCCESS;
  18. }

Thanks
You have two problems. First, when trying to write a double to a file, it's a good idea to use the value of the double and not the value of the stream.
  1. binf.write(reinterpret_cast<char*>(&pi), sizeof(double));
Second, two-way streams are tricky to use. It's better to just use two one-way streams so you don't have to worry about seeking at the right time or flushing or any of that stuff. Your code should work like this.
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. double pi = 3.14;
  9. double read;
  10. fstream binf("atest.txt", ios::binary|ios::in|ios::out);
  11. binf.write(reinterpret_cast<char*>(&pi), sizeof(double));
  12. binf.seekg( ios::beg );
  13. binf.read(reinterpret_cast<char*>(&read), sizeof(double));
  14. cout<< "\n" << read << "\n";
  15. return 0;
  16. }
But I would do it like this.
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. ofstream ofs( "atest.txt", ios::binary );
  10.  
  11. if ( ofs ) {
  12. double pi = 3.14;
  13.  
  14. ofs.write( reinterpret_cast<char*>( &pi ), sizeof pi );
  15. // Close the file to unlock it
  16. ofs.close();
  17.  
  18. // Use a new object so we don't have to worry
  19. // about error states in the old object
  20. ifstream ifs( "atest.txt", ios::binary );
  21. double read;
  22.  
  23. if ( ifs ) {
  24. ifs.read( reinterpret_cast<char*>( &read ), sizeof read );
  25. cout << read << '\n';
  26. }
  27. }
  28.  
  29. return 0;
  30. }
It's also good to mention that the file you produce is only guaranteed to be readable by a program written using the same version of the same compiler that the output program was written with. A lot of people don't like that portability issue, and will read and write their binary values manually instead of using read() and write().

Did I mention that text is probably a better choice than binary?
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC