By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,132 Members | 1,948 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,132 IT Pros & Developers. It's quick & easy.

General File Handling (Class Structure Preservation) Question

P: n/a
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:
...
};
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.