[comp.os.msdos.programmer] Are streams w/n classes flaky?

jamesh@cs.umr.edu (James Hartley) (11/28/90)

I recently tried to simulate persistent objects in C++ with the code given 
below using Turbo C++.  The results that I got were quite erratic.  Has
anyone else has problems with declaring and using streams within classes?

---------------------------- BEGIN CODE ------------------------------------

#include <fstream.h>
#include <stdlib.h>
#include <string.h>

class persist {
    char *fname;
protected:
    fstream dskfile;
public:
    persist(char *fn) {
        fname = new char[strlen(fn) + 1];
        strcpy(fname, fn);
  	dskfile.open(fname, ios::in | ios::out);
        if (!dskfile) {
	    cerr << "error in opening file\n";
	    exit(1);
	}
    }
    ~persist(void) {
	dskfile.close();
	delete fname;
    }
};

class constant : public persist {
    unsigned int k;
public:
    constant(char *fn) : persist(fn)  { dskfile >> k; }
    unsigned int get_constant(void)  { return k; }
};

main() {
    constant k("CONSTANT.TXT");
    cout << "k = " << k.get_constant << "\n";
    return 0;
}

----------------------------  END CODE  ------------------------------------

-- 
James J. Hartley                      Internet: jamesh@cs.umr.edu
Department of Computer Science          Bitnet: jamesh@cs.umr.edu@umrvmb.bitnet
University of Missouri - Rolla            UUCP: ...!uunet!cs.umr.edu!jamesh