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; ("example.txt");
  myfile << "Writing this to a file.";

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?


Member Avatar

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));
Be a part of the DaniWeb community

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