0

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
}
2
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
0

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));
0

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

0

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.

0

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.

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.