Thread: Reading a class object from a file...

  1. #1
    Registered User
    Join Date
    Mar 2010

    Question Reading a class object from a file...


    How can I read a class object from a file that I have just written to.

    The class
    class Hired
        int      nr;                       
        int      ant_barn;                 
        Hired (int n)
            cout << "Hired nr: (100-999)" << endl;
            cin >> nr;
            cout << "Ant barn: 0-20" << endl;
            cin >> ant_barn;
        Hired ()
            cout << "\n\nWARNING 1: SHALL NOT RUN THIS CONSTRUCTOR\n\n";
        void write_file(ofstream* out)
            *out << nr << " ";
            *out << ant_barn << " "<< endl;
        void display()
            cout << "Nr: " << nr;
            cout << "ant barn: " << ant_barn;
            cout << endl << endl;
        Hired(int nr, int b)
            nr = nr;
            ant_barn = b;
        int getnr()
                return nr;
        int getbarn()
                return ant_barn;
        void read_file(ifstream* in, Hired* a)
            int ii = 0;
  *) &a, sizeof(Hired));
            while (!in.eof())       
      *) &a, sizeof(Hired));
                hired2[ii] = new Hired(a->getnr(), a->getbarn());
    //Globale variabler
    Hired* hired[MAX_ANS+1];     //  Array with pointer.
    Hired* hired2[MAX_ANS+1];    //  Array with pointer
    int lastused = 0;
    The main programme:

    int main()
        hired[lastused] = new Hired(25); //Filling in data, at least 1
        ofstream myfile("hired.dta", ios::out | ios::ate);
        int i=0;                    
        //Read to file
        for (i = 1; i <= lastused; i++)
    //So far, so good...
    //Read the whole file, and put it back to memory
        ifstream myfile2("hired.dta", ios::in);
        Hired* a;
        Read_file(myfile2, a);
        cout << "DONE";
        return 0;
    SO; what this does (some things might not be here, because I've tried to minimize it, cutting out irrelevant code), it simply creates one object of the class "hired", puts the data from it (two int variables) into a text file.
    Now I want to read those two int variables back, and put it where it belongs (into an object of the class Hired).

    Whats working: Writing to the textfile, putting all object data to it.

    I've marked a red line... Where the erorr lies... I can compile this, but when running, just simply freezes when I reach the red lines...
    PS: should I rather write to a binary file rather than ASCII?
    Last edited by ManyTimes; 03-18-2010 at 07:21 PM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    1) Don't put interface code in an object unless that object exists solely to provide an interface. (Don't put calls to `std::cin' or `std::cout' in your class constructor.)

    2) If you don't want a default constructor, label the default constructor `private' so you simply can't call it.

    3) The `istream & std::istream::read(char *, streamsize)' method does not allocate memory. You are passing unallocated, and uninitialized, data to this method.

    4) You have a "write to file" method. Where is your "read from file" method?

    5) You are writing two `int' values without a separator. In "text mode", it is impossible to distinguish between two desperate values, in many cases, without a separator. You need to add a separator if you intend to use "text mode". (Consider the values "114" and "248"; without a separator it looks rather like "114248".)

    The problem here is the approach. If you don't want to create an object until you know you have good values to construct it with, that's great, but you can't do that with an uninitialized pointer.

    Write the "read from file" method, mirroring your "write to file" method, and we'll see where you get from there.


  3. #3
    Registered User
    Join Date
    Mar 2010
    Thanks for responding.
    1. Ok, next time I'll do that!

    2. Nice thinking, of course privatize it!

    3. Huh? So what should I do then? I try to read from the file a "line" in the size of my class "Hired"...?

    4. Write file method is in my class, my read file method is in the main, for now...

    5. Oh, I'll be darned, I just copied it from the wrong .cpp file...
    Of course I have a space in there! But then again, thats not my "problem", fixing small things are no problem, jsut want to make it WORK!

    Kk, edited first post with code... And cant really "mirroring Write_file" method, because this should actually read every class objects in the file, should be a global function, which it is now?

    PS : Is reading /writing with binary files easier?
    PS2: Was thinking here, should I store the "array amount" (lastused) in the file; at the very top? Or is this nonsense to store such a number in the file, because of reading the objects one by one will result in a filled array as it was before..ehm, drowned in my own thoughts here, sorry.

    Also; since I am using the "read" method, shouldnt I be using "write" method to write to the file, instead of "*out >> variable;" ?
    Last edited by ManyTimes; 03-18-2010 at 07:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

Tags for this Thread