How can I write a class with "string or char*" member variable to a file...?

B

babakandme

Hi to every body...:D
I'm a novice C++ programmer & I've a question,


How can I write a class as follow to a file...

Class A
{
public:
// Accessors Methods...
.
.
.
private:
int itsAge;
string itsName;
// char itsName[ 80 ];
};

I know that, I should use ofstream &....
but there is a problem:
"itsName" is a "string or char*" & It can not be written in desired
file...

Should I write a self writting method for this class to write itself
to a file or what?

thanks in advance...
 
G

Guest

Hi to every body...:D
I'm a novice C++ programmer & I've a question,


How can I write a class as follow to a file...

Class A
{
public:
// Accessors Methods...
.
.
.
private:
int itsAge;
string itsName;
// char itsName[ 80 ];
};

I know that, I should use ofstream &....
but there is a problem:
"itsName" is a "string or char*" & It can not be written in desired
file...

Actually it is a char array, and nothing else.
Should I write a self writting method for this class to write itself
to a file or what?


Decide on a file-format for writing you class, then implement

std::eek:stream& operator(std::eek:stream& o, const A& a);

so that it writes the members of A in the correct format. For more
information search for serialisation, also take a look in the FAQ:
http://www.parashift.com/c++-faq-lite/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,047
Members
47,646
Latest member
xayaci5906

Latest Threads

Top