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

Member Avatar for jencas

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();
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.