Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > General File Handling (Class Structure Preservation) Question

Reply
Thread Tools

General File Handling (Class Structure Preservation) Question

 
 
Sean W. Quinn
Guest
Posts: n/a
 
      12-01-2003
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:
...
};
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-01-2003
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
General....very general.... no important for u forever hi Python 0 03-18-2009 08:21 AM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 2 10-31-2007 02:58 AM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 1 10-30-2007 11:01 PM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 4 10-30-2007 08:21 PM
How to read a Structure from a matlab file to a structure in Vc++ 2003 meisterbartsch C++ 2 06-12-2007 08:47 PM



Advertisments