Check return values of fscanf() (just bail early if it fails)
Don't use titles as scanf templates, compiler can't seem to figure out that
these come from string literals. Insteadd use fscanf(..., strlen...)
read_high_score_table()
{
FILE *f;
read_high_score_table()
{
FILE *f;
f = open_score_file("r");
if(f) {
// If the file exists, read from it
for(j=0; j<2; j++) {
f = open_score_file("r");
if(f) {
// If the file exists, read from it
for(j=0; j<2; j++) {
+ ret = fseek(f, strlen(titles[j]), SEEK_CUR);
+ if (ret != 0) {
+ break;
+ }
for(i = 0; i<N_SCORES; i++) {
for(i = 0; i<N_SCORES; i++) {
- fscanf(f, "%d %31[^\n]\n", &g_scores[j][i].score, g_scores[j][i].name);
+ ret = fscanf(f, "%d %31[^\n]\n", &g_scores[j][i].score, g_scores[j][i].name);
+ if (ret != 2) {
+ break;
+ }
if(f) {
// If the file exists, write to it
for(j=0; j<2; j++) {
if(f) {
// If the file exists, write to it
for(j=0; j<2; j++) {
+ fprintf(f, "%s", titles[j]);
for(i = 0; i<N_SCORES; i++) {
fprintf (f, "%d %.31s\n", g_scores[j][i].score, g_scores[j][i].name);
}
for(i = 0; i<N_SCORES; i++) {
fprintf (f, "%d %.31s\n", g_scores[j][i].score, g_scores[j][i].name);
}