hi,

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
{

public: 

int a;
int b;

protected:

void function1();
void function2();

private:
int c
}

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

ofstream out("filename");
out.write((char*)&a,sizeof(int));
out.write((char*)&b,sizeof(int));

yes but if in a i had a string field??i would write::
out.write((char*)&str,sizeof(string));??????

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.