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)

#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 ;)

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.

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)

#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 ;)

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

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.

#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;
}

But I would do it like this.

#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;
}

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? :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.