Solved

General routine for writing and reading classes to file

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now