473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 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 3236
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.