| | |
Reading or writing a double into a bin file
![]() |
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)
Thanks
... 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)
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv[]) { double pi = 3.14; double read; ofstream binf1("bin.txt"); binf1.close(); fstream binf("bin.txt", ios::binary|ios::in|ios::out); binf.write(reinterpret_cast<char*>(&binf), sizeof(double)); binf.read(reinterpret_cast<char*>(&read), sizeof(double)); cout<< "\n" << read << "\n"; system("PAUSE"); return EXIT_SUCCESS; }
Thanks
Last edited by fesago90; Jan 24th, 2007 at 10:11 pm.
http://www.ulteo.com
Ulteo - Taste a bit of freedom
Ulteo - Taste a bit of freedom
C++ Syntax (Toggle Plain Text)
binf.write(reinterpret_cast<char*>(&binf), sizeof(double));

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."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
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)
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv[]) { double pi = 3.14; double read; ofstream binf1("bin.txt"); binf1.close(); fstream binf("bin.txt", ios::binary|ios::in|ios::out); binf.write(reinterpret_cast<char*>(&binf), sizeof(double)); binf.read(reinterpret_cast<char*>(&read), sizeof(double)); cout<< "\n" << read << "\n"; system("PAUSE"); return EXIT_SUCCESS; }
Thanks

C++ Syntax (Toggle Plain Text)
binf.write(reinterpret_cast<char*>(&pi), sizeof(double));
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv[]) { double pi = 3.14; double read; fstream binf("atest.txt", ios::binary|ios::in|ios::out); binf.write(reinterpret_cast<char*>(&pi), sizeof(double)); binf.seekg( ios::beg ); binf.read(reinterpret_cast<char*>(&read), sizeof(double)); cout<< "\n" << read << "\n"; return 0; }
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> int main() { using namespace std; ofstream ofs( "atest.txt", ios::binary ); if ( ofs ) { double pi = 3.14; ofs.write( reinterpret_cast<char*>( &pi ), sizeof pi ); // Close the file to unlock it ofs.close(); // Use a new object so we don't have to worry // about error states in the old object ifstream ifs( "atest.txt", ios::binary ); double read; if ( ifs ) { ifs.read( reinterpret_cast<char*>( &read ), sizeof read ); cout << read << '\n'; } } return 0; }
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.
![]() |
Similar Threads
- Writing to binary file, erases previous data? (C++)
- Win 98se & bios update with Bin file (Windows 95 / 98 / Me)
- File Operations (C++)
- Saving to a file from an array in C ? (C)
- Reading and Writing file (C)
- Need help with my console database app, program reading or writing incorrectly (C++)
Other Threads in the C++ Forum
- Previous Thread: Help make Console App Open in fullscreen.
- Next Thread: Need Help with this program:PLz Help
Views: 13627 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary c++ c/c++ calculator char class classes code command compile compiler console constructor conversion convert count data delete desktop dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui homework http i/o iamthwee image input int lazy library linker list loop looping math matrix member memory newbie number numbers object objects opengl output parameter pointer pointers problem program programming project random read recursion recursive reference search sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual visualstudio void win32 window windows winsock






