472,954 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

General File Handling (Class Structure Preservation) Question

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
1 3213
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rob Ristroph | last post by:
I have tried out PHP 5 for the first time (with assistance from this group -- thanks!). The people I was working with have a site that uses lots of php objects. They are having problems with...
39
by: Antoon Pardon | last post by:
I was wondering how people would feel if the cmp function and the __cmp__ method would be a bit more generalised. The problem now is that the cmp protocol has no way to indicate two objects are...
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
12
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
4
by: Jeff Johnson [MVP: VB] | last post by:
Pardon the crosspost, but I couldn't find a generic .NET group for VS.NET. I was wondering about the directory structure that the IDE creates when making a project. What's the point of the bin...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.