Don't use fscanf; it's brittle. Instead, read lines manually and use sscanf.

This commit is contained in:
Glenn Maynard
2003-03-27 21:23:56 +00:00
parent 522fbab465
commit 6e9eb566cd
+33 -15
View File
@@ -25,6 +25,8 @@
#include "ThemeManager.h" #include "ThemeManager.h"
#include "GameManager.h" #include "GameManager.h"
#include <fstream>
SongManager* SONGMAN = NULL; // global and accessable from anywhere in our program SongManager* SONGMAN = NULL; // global and accessable from anywhere in our program
@@ -34,7 +36,7 @@ const CString NOTES_SCORES_FILE[NUM_MEMORY_CARDS] = { "Player1NotesScores.dat",
const CString COURSE_SCORES_FILE[NUM_MEMORY_CARDS] = { "Player1CourseScores.dat", "Player2CourseScores.dat", "MachineCourseScores.dat" }; const CString COURSE_SCORES_FILE[NUM_MEMORY_CARDS] = { "Player1CourseScores.dat", "Player2CourseScores.dat", "MachineCourseScores.dat" };
const int CATEGORY_RANKING_VERSION = 1; const int CATEGORY_RANKING_VERSION = 1;
const int COURSE_RANKING_VERSION = 1; const int COURSE_RANKING_VERSION = 1;
const int NOTES_SCORES_VERSION = 1; const int NOTES_SCORES_VERSION = 2;
const int COURSE_SCORES_VERSION = 1; const int COURSE_SCORES_VERSION = 1;
@@ -302,37 +304,54 @@ void SongManager::InitMachineScoresFromDisk()
// notes scores // notes scores
for( int c=0; c<NUM_MEMORY_CARDS; c++ ) for( int c=0; c<NUM_MEMORY_CARDS; c++ )
{ {
FILE* fp = fopen( NOTES_SCORES_FILE[c], "r" ); ifstream f(NOTES_SCORES_FILE[c]);
if( fp ) if( f.good() )
{ {
CString line;
getline(f, line);
int version; int version;
fscanf(fp, "%d\n", &version ); sscanf(line.GetString(), "%i", &version);
if( version == NOTES_SCORES_VERSION ) if( version == NOTES_SCORES_VERSION )
{ {
while( fp && !feof(fp) ) while( f && !f.eof() )
{ {
char szSongDir[256]; CString sSongDir;
getline(f, sSongDir);
getline(f, line);
unsigned uNumNotes; unsigned uNumNotes;
fscanf(fp, "%[^\n]\n%u\n", szSongDir, &uNumNotes); sscanf(line.GetString(), "%u", &uNumNotes);
Song* pSong = this->GetSongFromDir( szSongDir );
Song* pSong = this->GetSongFromDir( sSongDir );
for( unsigned i=0; i<uNumNotes; i++ ) for( unsigned i=0; i<uNumNotes; i++ )
{ {
NotesType nt; NotesType nt;
Difficulty dc; Difficulty dc;
char szDescription[256];
fscanf(fp, "%d\n%d\n%[^\n]\n", &nt, &dc, szDescription); getline(f, line);
sscanf(line.GetString(), "%d %d", &nt, &dc);
CString sDescription;
getline(f, sDescription);
Notes* pNotes = NULL; Notes* pNotes = NULL;
if( pSong ) if( pSong )
{
if( dc==DIFFICULTY_INVALID ) if( dc==DIFFICULTY_INVALID )
pNotes = pSong->GetNotes( nt, szDescription ); pNotes = pSong->GetNotes( nt, sDescription );
else else
pNotes = pSong->GetNotes( nt, dc ); pNotes = pSong->GetNotes( nt, dc );
}
int iNumTimesPlayed; int iNumTimesPlayed;
Grade grade; Grade grade;
float fScore; float fScore;
fscanf(fp, "%d %d %f\n", &iNumTimesPlayed, &grade, &fScore );
getline(f, line);
sscanf(line.GetString(), "%d %d %f\n", &iNumTimesPlayed, &grade, &fScore );
if( pNotes ) if( pNotes )
{ {
pNotes->m_MemCardScores[c].iNumTimesPlayed = iNumTimesPlayed; pNotes->m_MemCardScores[c].iNumTimesPlayed = iNumTimesPlayed;
@@ -342,7 +361,6 @@ void SongManager::InitMachineScoresFromDisk()
} }
} }
} }
fclose(fp);
} }
} }
@@ -474,7 +492,7 @@ void SongManager::SaveMachineScoresToDisk()
Notes* pNotes = vNotes[i]; Notes* pNotes = vNotes[i];
ASSERT(pNotes); ASSERT(pNotes);
fprintf(fp, "%d\n%d\n%s\n", fprintf(fp, "%d %d\n%s\n",
pNotes->m_NotesType, pNotes->m_NotesType,
pNotes->GetDifficulty(), pNotes->GetDifficulty(),
pNotes->GetDescription().c_str() ); pNotes->GetDescription().c_str() );