Wait, are you saying you're reading a standard ASCII text file? :confused: If so, why bother reading it as binary, byte by byte? Just open a normal file stream and use its standard operator >> to stream the numbers into your variables. Try these functions to write and read your
void WriteScores(const std::vector& scores) {
int nscores = (int) scores.size();
std::ofstream scorestream("Myscores.txt");
scorestream << nscores; // write number of entries
for (int i=0; i < nscores; ++i)
scorestream << ' ' << scores[i]; // write entries, using blank (' ') as separator
}
int ReadScores(std::vector& scores) {
int nscores = 0;
std::ifstream scorestream("Myscores.txt");
scorestream >> nscores;
scores.resize(nscores);
for (int i=0; i < nscores; ++i)
scorestream >> scores[i];
return nscores;
}
You may want to add in some extra code to check for premature end of file or failure to create/write or read the file. You might also want to check the state of the filestream after reading each number to make sure an actual integer was read (if there was no integer, the resulting value will be 0, but since 0 may be a valid value, you can check on the state of your stream variable instead)