| | |
Reading or writing a double into a bin file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 11: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."
•
•
•
•
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
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






