Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

General routine for writing and reading classes to file

Posted on 1997-06-02
1
Medium Priority
?
195 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

670 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