Loading/Saving Objects

This is a discussion on Loading/Saving Objects within the C++ Programming forums, part of the General Programming Boards category; I have a base class "Animal", it has two derived classes, "Dog" and "Cat". As my program creates new objects ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    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?

    Example:

    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,
    Alan

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

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

    Post Arrays!

    Just create an array of objects:

    Dog *pDog[2] = new Animal;

    or something like that.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    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
    Location
    CA
    Posts
    2,459
    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
    Posts
    1,065
    You could just use an array of your objects and use WriteFile to save the data to file.

    Code:
    MyAnimal newAnimal[2];
    
    long lSize=sizeof(MyAnimal);
    ULONG fp=0;
    HANDLE hFile=CreateFile("Animal.dat",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    for(int i=0;i<2;i++)
     {
        fp=i*lSize;
        WriteFile(hFile,&newAnimal[i],lSize,&fp,NULL);
     }
    CloseHandle(hFile)
    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, 11:10 PM
  3. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 11:01 AM
  4. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 06:57 PM
  5. Objects, or pointers to objects?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2001, 12:57 AM

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