I have file and it contents name, surname and age.
john doe 20
lionel messi 33
cristiano ronaldo 35
What I want to do is to get specific person by his/her age and updating any information of him/her. In the sample code below, I want to find a person by his/her age change it.
#include <stdio.h>
#include <string.h>
int main () {
    char line[30];
    FILE *fp;
    int age;
    if (!(fp = fopen("test.txt", "rb"))) {  /* validate file open for reading */
        perror ("file open failed");
        return;
    }
    char name[20], surname[20];
    
    while (fgets (line, 30, fp)) {
        int off = 0;
        while (sscanf (line, "%s%s%d%n", name, surname, &age, &off) == 3) {
            if(age == 35) {
                fseek(fp,-2, SEEK_CUR);
                printf("%s", name);
                fprintf(fp, "%d", 19);
            }
            strcpy(line, line+off);
        }
    }
    fclose(fp);
    return 0;
}
However, my code does not work as expected. Sometimes it updates the information but sometimes not. In addition, I used this and this links to create this program. Thx in advance for your helpful comments.
 
    