Solved

General routine for writing and reading classes to file

Posted on 1997-06-02
1
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
michelv@unit4.nl earned 200 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 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