How to read a formated chain until a certain point in C?

39 Views Asked by At

I am currently struggling with file manipulations in C and still have a lot of reflexes to acquire. My code is here :

int main(int argc, char *argv[])
{
    FILE* f = fopen("/Users/[...].txt","r+");
    int input1=0; int input2=0; int input3=0;
    
    fscanf(f,"sommets : %d\n",&input1);
    printf("%d\n",input1);
    fscanf(f,"transitions : %d\n",&input1);
    printf("%d\n",input1);
    printf("%ld\n",ftell(f));
    fscanf(f,"ARCS PRE\n");
    printf("%ld\n",ftell(f));
    long int point = ftell(f);
    char word[10]="ARCS POST"; char data[100];
    while((strcmp(word, fgets(data,100,f)))!=0){ 
        fseek(f,point-ftell(f), (int) ftell(f));  //to reposition the cursor after strcmp
        fscanf(f, "%d->%d,%d\n", &input1, &input2, &input3);
        printf("%d->%d,%d\n",input1, input2, input3);
        point =ftell(f);
    }
    fclose(f);
    
    return 0;
} 

The part which is not working is around the while : here is the file I try to read

sommets : 69
transitions : 9
ARCS PRE
1->2,5
1->3,7
ARCS POST
1->7,1
1->9,4
END

The while is supposed to stop the reading once I reach "ARCS POST" in the file but never recognizes such a string, and it seems that the fseek in the loop doesn't want to work. In fact I also tried with SEEK_CUR but its value seems stuck to 1. Does anyone have an alternative for what I am trying to do ? Or does anyone know why nothing goes on this part as intended ?

0

There are 0 best solutions below