i want to binary write the fields of class to a file.Mainly i want to serialize only the public fields not the private ones or the functions....

class a


int a;
int b;


void function1();
void function2();

int c

use ofstream's write() method to write out each individual object, for example

ofstream out("filename");

yes but if in a i had a string field??i would write::

strings are written to binary files exactly like they are to text files including the '\n' at the end to identify the end of the string.

so i would use out<<mystring<<endl?

so i would use out<<mystring<<endl?

Yes. Suggestion: write yourself a small test program that does nothing more than open a binary file for output, write some data to it, close the file, then open a binary file for input and read the data back in. That will teach you how binary files behave.

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.