How do Iload values back into class from file?

Started by
9 comments, last by Zahlman 18 years, 3 months ago
I need to load the data that I am saving to the file back into the classes. How would I do that? Here is the code I use to save the data to file:

void SaveScores(const vector<Score> & scoreList)
{
     vector<Score>::const_iterator theIter;
     
     ofstream scoreFile("scores.txt");
     if(!scoreFile)
                   cout << "Unable to open 'scores.txt' for writing.\n";
     else
     {
             for(theIter=scoreList.begin(); theIter!=scoreList.end(); theIter++)
                                            scoreFile << theIter->GetInitials() << " " << theIter->GetDiff() << " " << theIter->GetGuesses() << "\n";
             scoreFile.close();
             cout << "Scores saved successfully.\n";
     }
}


Advertisement
#include <sstream>int toInt( std::string& str ){    stringstream stream;    stream << str;    int i;    stream >> i;    return i;}void LoadScores( vector<Score> & scoreList ){        ifstream scoreFile("scores.txt");     if(!scoreFile)             cout << "Unable to open 'scores.txt' for reading.\n";     else     {             vector<string> textScores;             while( scoreFile.good() )             {                 string currentScore;                 scoreFile >> currentScore;                 textScores.push_back( currentScore );             }             // make sure we have multiple of 3             if( textScores.size() % 3 != 0 )             {                //handle corrupt file, exception or something             }             for( int i = 0 ; i < textScores.size() ; i += 3 )             {             // has a constructor?             Score score( textScores,toInt(textScores[i+1]),toInt(textScores[i+2]) );             // otherwise             Score score;             score.setInitials( textScores );             score.setDiff( toInt(textScores[i+1]) );             score.setGuesses( toInt(textScores[i+2]) );             scoreList.push_back( score );             }                       scoreFile.close();             cout << "Scores loaded successfully.\n";     }}
Quote:Original post by rip-off
*** Source Snippet Removed ***


Simplified...
    void LoadScores( vector<Score> & scoreList )    {           ifstream scoreFile("scores.txt");        if(!scoreFile)            cout << "Unable to open 'scores.txt' for reading.\n";        else        {            while( scoreFile.good() )            {                string initials;                int diff;                int guesses;                scoreFile >> initials >> diff >> guesses;                scoreList.push_back( Score( initials, diff, guesses ) );            }        }    } 
(assumes that the initials don't have spaces in them)
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Thanks guys, I wasnt able to get onto the forums for some reason and I came up with this:

//Load scores from previous sessionvoid LoadScores(vector<Score> & scoreList){     vector<Score>::iterator theIter;     int theGuesses;     string theInitials;     string theDiff;          ifstream scoreFile("scores", ios::binary);     if(scoreFile)     {         for(theIter=scoreList.begin(); theIter!=scoreList.end(); theIter++)         {                                        scoreFile >> theInitials;                                        scoreFile >> theDiff;                                        scoreFile >> theGuesses;                                        theIter->SetAll(theDiff, theInitials, theGuesses);         }     }}


Ty for the help though.
Does that work if the scoreList is empty?
If you want to load stuff from a class you should take a look at writing the file in binary instead of text.

Check this out. I only skimed over it but it looks pretty good.
F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
Quote:Original post by pulpfist
Does that work if the scoreList is empty?


Yes. For an empty container, .begin() == .end(), so the loop bails immediately and correctly does nothing.

Quote:Original post by DigiDude
If you want to load stuff from a class you should take a look at writing the file in binary instead of text.


o rly?
Quote:Original post by Zahlman
Quote:Original post by pulpfist
Does that work if the scoreList is empty?


Yes. For an empty container, .begin() == .end(), so the loop bails immediately and correctly does nothing.

Quote:Original post by DigiDude
If you want to load stuff from a class you should take a look at writing the file in binary instead of text.


o rly?
Is there something wrong with using binary? It would seem easier to me. Just write out a class then read it back in and use it to fill a class of the same type.

F-R-E-D F-R-E-D-B-U-R...G-E-R! - Yes!
You have to worry about byte-alignment and Endianness. If you do use binary data, it is safer to write each component of the class individually.
Quote:
Yes. For an empty container, .begin() == .end(), so the loop bails immediately and correctly does nothing.


If the file is not empty, that would not be the correct behaviour I think.
However, I saw the rest of the source in another thread, so I understand why this works now.

This topic is closed to new replies.

Advertisement