Sign in to follow this  

How do Iload values back into class from file?

This topic is 4355 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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";
     }
}


Share this post


Link to post
Share on other sites

#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[i],toInt(textScores[i+1]),toInt(textScores[i+2]) );
// otherwise
Score score;
score.setInitials( textScores[i] );
score.setDiff( toInt(textScores[i+1]) );
score.setGuesses( toInt(textScores[i+2]) );
scoreList.push_back( score );
}
scoreFile.close();
cout << "Scores loaded successfully.\n";
}
}

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Thanks guys, I wasnt able to get onto the forums for some reason and I came up with this:


//Load scores from previous session
void 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by DigiDude
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.


I really, really hope you don't mean something like this:


class Foo {
Bar* myBar;
};

// ...
myFile.write(reinterpret_cast<char*>(&myFooInstance, sizeof(Foo));
// ...
myFile.read(reinterpret_cast<char*>(&myFooInstance, sizeof(Foo));
// Oops! That memory location held an appropriate Bar during the last program
// run at the time of the write() call, but it could be *anything* now.


But really, read the link.

Share this post


Link to post
Share on other sites

This topic is 4355 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this