General File Handling (Class Structure Preservation) Question

Discussion in 'C++' started by Sean W. Quinn, Dec 1, 2003.

  1. Hey folks,

    I have a question regarding file handling, and the preservation of class
    structure. I have a class (and I will post snippets of code later in the
    post) with both primitive data structures (ints), and more complex data
    structures (strings and vectors) in it, and would like to write the entire
    class to a data file that could then
    be read back and loaded.

    However I'm having difficulty with this -- I found out (due to an oversight
    when I was first coding it) that I was trying to write the pointer data of
    the vectors as is, and although (I think by a fluke) the data was showing
    properly, MSVC++ 6.0 was giving me an assertion failure warning at run time
    and erroring out. Although, I did notice that g++ compiled it, and ran it
    with no problems. I've since attempted to implement code that helps with
    the vector handling to the ostream, but I'm not entirely certain it is
    working properly.

    The following are the overloaded functions I have written for both in and
    out stream operations ( << and >> ).

    // to handle writing the entire class (preserving structure et. al.) to the
    data file.. (placed in my item.h) file
    template< class T >
    fstream &operator<< ( fstream &outFileStream, T &obj )
    {
    outFileStream.write( (const char*) &obj, sizeof( T ) );
    return(outFileStream);
    }

    // to handle reading back in the data that was exported from the class
    template< class T >
    fstream &operator>> ( fstream &inFileStream, T &obj )
    {
    inFileStream.read( (char*) &obj, sizeof( T ) );
    return(inFileStream);
    }

    // overloaded << operator for vectors... (placed in my property.h file)
    template <class T>
    ostream &operator<< ( ostream &outStream, vector<T> &v)
    {
    for (vector<T>::const_iterator i = v.begin(); i != v.end(); i++)
    outStream << i.element();
    return(outStream);
    }

    When I was writing them, I think that I had the idea that my overloaded
    fstreams would go through each member, and perform the appropriate writing
    procedure for each member object, until it came to say the vector, where I
    needed to specify how it would be dealt with in a stream. I'm not certain
    this is the right idea now though.

    Another two questions which have come up are the following: first off,
    regarding the templated Vector ostream function... what does the following
    code do `i.element()' I can't seem to find any documentation for it. Is
    element part of the iterator? Or is it a member function in vector?

    Second question is the following, do I have to design an overloaded ostream
    (and istream eventually) function like I did for my vector functions, for
    strings as well? Or are their internally define stream operators
    sufficient?

    I'd like to avoid writing the pieces individually to a file and tagging
    them, if I can -- I'd prefer the internal structure be kept intrinsic to the
    file everything is being saved to, if at all possible.

    I guess what I am asking is, can anyone take a look at this code; tell me if
    I am going about the file handling properly for preserving class/object
    structure, and any suggestions that you might have. I'm a very
    visual/by-example sort of learner, so if you can give me an example of what
    you mean when making suggestions, that would be very gratefully appreciated!

    Thanks for the time!
    Sean W. Quinn

    [------------ code snippet of item.h ------------------]

    class item
    {
    private:
    int mID;
    string mName;
    string mDesc;
    int mCost;
    vector<property> mProperties;

    public:
    ...
    };

    [------------ code snippet of property.h ------------------]

    class property
    {
    private:
    string mType;
    vector<effect> mEffects;

    public:
    ...
    };

    [------------ code snippet of effect.h ------------------]

    typedef enum duration_type__ { temporary, permenant } duration_type;

    class effect
    {
    private:
    string mTarget;
    string mTargetEffect;
    int mModifier;
    duration_type mDuration;
    public:
    ...
    };
     
    Sean W. Quinn, Dec 1, 2003
    #1
    1. Advertising

  2. Sean W. Quinn wrote:
    > Hey folks,
    >
    > I have a question regarding file handling, and the preservation of class
    > structure. I have a class (and I will post snippets of code later in the
    > post) with both primitive data structures (ints), and more complex data
    > structures (strings and vectors) in it, and would like to write the entire
    > class to a data file that could then
    > be read back and loaded.
    >
    > However I'm having difficulty with this --


    There are dozens of opinions and ways to do what you ask but, if you're
    trying to store objects persistently in a file by writing the entire
    image to a file, you're out of luck with vector<>. vector dynamically
    allocates storage.

    Look up the faq on serialization and google for some past articles on
    the subject.

    Personally, if I find that performance is a key factor, I use memory
    mapped files with special allocators that allocate objects directly in
    the file. But this is all off-topic here.
     
    Gianni Mariani, Dec 1, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Rogers
    Replies:
    0
    Views:
    1,315
    David Rogers
    Jun 28, 2003
  2. Chris
    Replies:
    1
    Views:
    388
    Damian Maclennan
    Jul 16, 2003
  3. Replies:
    1
    Views:
    687
    Richard Tobin
    Oct 4, 2005
  4. Grzegorz Adam Hankiewicz

    Looking for source preservation features in XML libs

    Grzegorz Adam Hankiewicz, Dec 28, 2004, in forum: Python
    Replies:
    1
    Views:
    356
  5. =?ISO-8859-1?Q?Gregory_Pi=F1ero?=

    Python syntax high-lighting and preservation on web

    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=, Jun 29, 2005, in forum: Python
    Replies:
    2
    Views:
    370
    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=
    Jun 29, 2005
Loading...

Share This Page