Thread: Loading/Saving Objects

  1. #1
    Registered User
    Join Date
    Oct 2001

    Loading/Saving Objects

    I have a base class "Animal", it has two derived classes, "Dog" and "Cat".

    As my program creates new objects during its run, how would you save an Object, or Load an object once it has been saved?

    I have checked all through my C++ book and have not found anything on Saving or loading Objects.

    I can send the data of an object to a file by just saying,

    fout << newDog;

    if the file has been opened for input using fout.

    Is this saving an Object? or is there more to it than meets the eye.

    If this is saving an object, how would I create the object upon retrieving the data from the saved file?


    My Animal class has the following private members,
    (Age, Name, Sound, Weight). When I fout << newAnimal to the file, my file would look like this:

    12 Spot Roof 27

    When I opened this file How would I load an object?

    Maybe i am looking to deep into a simple problem, I am fairly new to the C++ language.

    Thanks for any help you can provide,

  2. #2
    Registered User
    Join Date
    Sep 2001
    not sure if thatll work, try saving each part of the object.

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001

    Post Arrays!

    Just create an array of objects:

    Dog *pDog[2] = new Animal;

    or something like that.


    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    you might need to have your own manual function for data members of seperate instances of the same class to be copied... if you had a pointer to a structure of the data [all under one name] it may be easier for you...
    hasafraggin shizigishin oppashigger...

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    You could just use an array of your objects and use WriteFile to save the data to file.

    MyAnimal newAnimal[2];
    long lSize=sizeof(MyAnimal);
    ULONG fp=0;
    for(int i=0;i<2;i++)
    Then just use ReadFile to load it back from the file into your objects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  2. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  3. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  4. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM
  5. Objects, or pointers to objects?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2001, 12:57 AM