c++ object I/O

Started by
3 comments, last by thejakl007 21 years, 7 months ago
Ok, I know a class object can be read from file as well as written to file. Now most of the examples I have seen have only implemented this with a set of variables already declared. My question is if my class implements a linked list and I wish to write it to file like this: outfile.write(reinterpret_cast(&MyList),sizeof(MyList)); Will it write out all of the data contained in all of my nodes as well? Or would I need to write each of my nodes contained in a my list to file in a similar manner? Like this perhaps: outfile.write(reinterpret_cast(&Node),sizeof(Node)); Or am i doing this completely wrong. Any input would be great!! Thanks!!
Advertisement
Completely wrong.

To effectively serialize your objects (write them to file), you need to provide methods that either prepare them for writing or insert them into ostream objects. In the case of a linked list, you''d need to traverse the list and write the data at each node independently. Here''s a (much simplified) example:

  struct Data{  string text;  int number;};//template < typename T >class ListNode{  ListNode<T> * prev, * next; // doubly linked list  T payload;  //public:  friend ostream & operator << ( ostream &os, const ListNode<T> & n );  // constructor, other methods};//template < typename T >ostream & operator << ( ostream &os, const ListNode<T> & n ){  os << payload;  // insert necessary formatting here  return os;}//template < typename T >class List{private:  ListNode<T> * head, * tail; // bi-directional traversal  //public:  friend ostream & operator << ( ostream & os, const List<T> & l );  // constructor, other methods};//template < typename T >ostream & operator << ( ostream & os, const List<T> & l ){  ListNode<T> * n = head;  while( n )    os << *n, n = n->next;  return os;}  
At the risk of sounding dumb I have 2 questions.

The first is what is the purpose of "const ListNode & n" within the operator overloading?

quote:Original post by Oluseyi
template < typename T >
class ListNode
{
ListNode * prev, * next; // doubly linked list
T payload;
//
public:
friend ostream & operator << ( ostream &os, const ListNode & n );
// constructor, other methods
};
//
template < typename T >
ostream & operator << ( ostream &os, const ListNode & n )
{
os << payload;
// insert necessary formatting here
return os;
}




Second, when reading this data from the file would i just extract it the same way it was written straight into a node or would I have to parse it in any way? Each of my nodes contain at least 4 variables, if that makes a difference.
quote:Original post by thejakl007
At the risk of sounding dumb I have 2 questions.

It''s never dumb when you are sincerely seeking knowledge. Never.

quote:
The first is what is the purpose of "const ListNode<T> & n" within the operator overloading?

The purpose of the entire second parameter is to tell the insertion operator what type of object it will be inserting (so that the compiler can match them at compile time). The purpose of the const is to tell the compiler that the right hand parameter will not be modified, which helps you check for inappropriate code because the compiler will issue either a warning or an error if you try to mutate that const object.

quote:
Second, when reading this data from the file would i just extract it the same way it was written straight into a node or would I have to parse it in any way?

You''d have to parse it within the version of operator << overloaded for your node class.
Thanks!! I will give it a try.

This topic is closed to new replies.

Advertisement