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";
}
}
How do Iload values back into class from file?
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:
#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)
Thanks guys, I wasnt able to get onto the forums for some reason and I came up with this:
Ty for the help though.
//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.
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.
Check this out. I only skimed over it but it looks pretty good.
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 ZahlmanIs 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.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?
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
Popular Topics
Advertisement