Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

General routine for writing and reading classes to file

Posted on 1997-06-02
1
Medium Priority
?
198 Views
Last Modified: 2010-04-10
I am interested in coming up with a general method of
writing an ASCII representation of a class to a file.  
Currently I am doing something like this:


ofstream ofile;
ifstream ifile;

// please assume files are properly opened and closed
// within the read and write routines, etc.  This is just
// an abbreviated version...

class A
{
  public:
    int A1;
    float A2;

    A();
    ~A();

    write() { ofile << A1 << " " << A2 << endl; };
    read()  { ifile >> A1 >> A2; };
}

class B
{
  public:
    int B1;
    float B2;
    double B3;
    char B4;

    B();
    ~B();

    write() { ofile << B1 << " " << B2 << " " << B3 << " " << B4 << endl; };
    read()  { ifile >> B1 >> B2 >> B3 >> B4; };
}



... and I have to define a very specific write and read for
each class that will be written to the file.  As classes
are modified or changed, I need to make sure that the reads
and writes are updated and completely in synch or I will
have problems.


What I want to do is have some "system" type command which
will allow me to somehow go through a class and determine
the structure of that class at runtime and properly write
each element to file.

Has anyone ever seen of anything like this or perhaps
written something like this?  I would greatly appreciate
any advice or information anyone has.

Thanks in advance,

Rick Cannon
rick.cannon@aplmail.jhuapl.edu
0
Comment
Question by:cannon060297
1 Comment
 
LVL 1

Accepted Solution

by:
michelv@unit4.nl earned 400 total points
ID: 1163793
If your compiler supports the use of Run-time Type Information (RTTI) you _might_ be able to achieve a solution. I'm merely giving you a pointer to a technology here. I've got no specific experience in this field.

I would recommend using a standard library for automated class streaming. They are available everywhere and provide you with presistent classes, which I believe is what you're looking for.

It should also not be too much of a problem to write such a mechanism yourself using polymorphism. I believe the ARM gives an example of this.

-- Michel

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question