Persisting to a Flat File

Instead of using an ostream or an iostream as the target of the serialization process, an fstream (file stream object) can be used to persist to a flat file. The state of one or more objects may be persisted to the file. The name of the file and the placement of the file in the directory structure may be used by the persistence mechanism to keep track of a set of objects across applications.

Sample classes would have these interfaces:

class Writer
     {
     public:
        Writer(char *fileName):fout(fileName,ios::binary){};
        ~Writer() {fout.close();}
        virtual Writer& operator<<(int&);
        virtual Writer& operator<<(long&);
        virtual Writer& operator<<(short&);
        virtual Writer& operator<<(char*);

     private:
            ofstream fout;
     };

class Reader
     {
     public:
        virtual Reader& operator>>(int&);
        virtual Reader& operator>>(long&);
        virtual Reader& operator>>(short&);
        virtual Reader& operator>>(char*&);

        Reader(char *fileName):fin(fileName,ios::binary){}
        ~Reader(){fin.close();}

     private:
        ifstream fin;
     };

© 1998 by Wrox Press. All rights reserved.