need help

This is a discussion on need help within the C++ Programming forums, part of the General Programming Boards category; Does anyone have a good tutorial on howto save an object to file and restore it ?...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    need help

    Does anyone have a good tutorial on howto save an object to file and restore it ?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    To save one integer (in text):
    Code:
    void SaveData(int Data)
    {
       ofstream WriteFile;
    
       WriteFile.open("Save.dat", ios::out);
       if(!WriteFile.fail())
       {
          WriteFile << Data;
          WriteFile.close();
       }
    }
    
    int LoadData()
    {
       ifstream ReadFile;
       int Data = 0;
    
       ReadFile.open("Save.dat", ios::in);
       if(!ReadFile.fail())
       {
          ReadFile >> Data;
          ReadFile.close();
       }
       return Data;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

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