0

It seems that writing ascii files changed significantly from c to c++. It moved from file pointers and things like that to being very easy, like

ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.";
  myfile.close();

However, from googling it seems that binary file reading/writing has not become any easier - you still have to keep track of the sizeof() what you are writing and input using buffers and all that. Am I correct? or is there indeed an easy way that I just didn't find?

Thanks,
Dave

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by jencas
0

You're right! A small example:

class Data 
    {
        int    key;
        double value;
    };
    
    Data x;

    ofstream myFile ("data.bin", ios_base::out | ios_base::trunc | ios::binary);
    myFile.write ((char*)&x, sizeof (Data));
    myFile.close();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.