Solved

General routine for writing and reading classes to file

Posted on 1997-06-02
1
192 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

615 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