write/read list of classes to file

This is a discussion on write/read list of classes to file within the C++ Programming forums, part of the General Programming Boards category; I don't get it. I am working on a program in which i'd like to save a list of classes ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    6

    Question write/read list of classes to file

    I don't get it. I am working on a program in which i'd like to save a list of classes to a file, and later on read them again. I am doing something wrong but i dont know what. The code below is an example of what i want to do. When i compile the whole code everything seems to go well, but when i exclude the code between the 2 lines i get an acces violation or some other fault. I dont know if i am doing something wrong writing of reading of both. Can somebody please help with this problem, i have been trying to figure it out for a long time but can get it to work.
    Thnx

    #include <fstream.h>
    #include <string>
    using namespace std;

    class Word
    {
    public:
    void SetWord(string woord) { str = woord; };
    string GetWord() const { return str; };
    private:
    string str;
    };

    int main()
    {
    //---------------------------------------------------------------------
    Word * pWrd = new Word;
    pWrd->SetWord("It works");

    ofstream fout("test.bin", ios::binary | ios::out | ios::trunc);
    fout.write((char*) pWrd, sizeof (*pWrd));
    fout.close();
    //---------------------------------------------------------------------
    Word * uWrd = new Word;
    ifstream fin("test.bin", ios::binary | ios::in);
    fin.read((char*) uWrd, sizeof (*uWrd));

    cout << uWrd->GetWord() << endl;

    fin.close();
    return NULL;
    }

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    there is a very good reason you are having problems here. Your data is dynamic! the string class is holding a pointer to a char buffer that was dynamically created. You can't just save that pointer out to a file and expect it to work. Try something like adding a Save() function on the Word object. Like this maybe:

    Code:
    void Word::Save(ofstream &fout)
       {
       fout << str.c_str();
       }
    and then for reading back in:

    Code:
    void Word::Restore(ifstream &fin)
       {
       char tempstr[100];
       fin.read(tempstr,99);
       str = tempstr;
       }

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    6
    Thnx your advice, never would have figured it out myself

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21