I have a file which looks like this :
Code:
level 1
name hans
score 2
level 2
name peter
score 4
level 3
name me
score 3
If I break the record, I want to update the score in the file. I don't have a clue how to do it though. This is what I have so far :
Code:
void writeHighScore(HighScore *hs){
FILE *f;
char mod[12], value[12];
int ival, okWrite = 0;
f = fopen("test.dat", "r+");
if ( !f ) return;
while (!feof(f)) {
if (fscanf(f, "%s %s", mod, value) != 2) break;
if ( H_strCompare(mod, "level") ){
ival = atoi(value);
if (ival == G->field->level){
printf ("%s %s\n", mod, value);
okWrite = 1;
}
}
else if ( H_strCompare(mod, "name") ){
if (okWrite){
printf ("%s %s\n", mod, value);
//what to do here ?
}
}
else if ( H_strCompare(mod, "score") ){
if (okWrite){
printf ("%s %s\n", mod, value);
//what to do here ?
okWrite = 0;
}
}
}
fclose(f);
}
Is there a way to delete the contents of those lines, and replacing them by another one ?
Thanks in advance.