Hi, I need to do a program that ask the user to enter different stages and it must loop. The program writes in a .dat and read in it too. It also check if the .dat is empty or is even created to write after the last one in it. Before every name there is a number and they must follow. But there 's a problem when I enter stage 21, go back, reenter stage 21 and input 2 when asked "same stage or quit" it stops working.
My code :


Code:
#include <stdio.h>
    #include <stdlib.h>
    typedef struct formation{
    char name[21];
    struct formation *suivantf;
    }formation;


    int main(){
    FILE *fresf,*fdatf;
    int i,j,choice1,choice2,choice3,choice4,nf=1,nbf=0,size;
    char read[21];
    fresf=fopen("formationtest.dat","a");
    fdatf=fopen("formationtest.dat","r");
    formation *debf,*courantf,*suivantf;
    debf=malloc(sizeof(formation));


    printf("stage 1 (1), stage 2 (2) or quit (4)\n");
    scanf("%d",&choice1);
    while(choice1!=4){
        if(choice1==2){
        printf("stage 21 (1), stage 22 (2) or quit (4)\n");
        scanf("%d",&choice2);
        while(choice2!=4){
                if (choice2==1){
                    courantf=debf;
                    do{
                        if(nbf!=0){
                            fprintf(fresf,"\n");
                        }
                        i=1;


                        printf("name ?\n");
                        scanf("%20s",courantf->name);
                        if(fdatf!=NULL){
                            do{
                                fscanf(fdatf,"%2d ",&nf);
                                fgets(read,20,fdatf);
                            }while(!feof(fdatf));
                            nf++;
                            fseek(fdatf,0,SEEK_END);
                            size=ftell(fdatf);
                            if(size!=0){
                                fprintf(fresf,"\n");
                            }
                            fprintf(fresf,"%2d %p %-20s",nf,(void *) courantf,courantf->name);
                            suivantf=malloc(sizeof(formation));
                            courantf->suivantf=suivantf;
                            fprintf(fresf,"%p",(void *) courantf->suivantf);
                            courantf=suivantf;
                            nbf++;
                            printf("same stage (1) or quit (2)?\n");
                            scanf("%d",&choice3);
                        }
                        else{
                            fprintf(fresf,"%2d %p %-20s",nf,(void *) courantf,courantf->name);
                            suivantf=malloc(sizeof(formation));
                            courantf->suivantf=suivantf;
                            fprintf(fresf,"%p",(void *) courantf->suivantf);
                            courantf=suivantf;
                            nbf++;
                            nf++;
                            printf("same stage (1) or quit (2)?\n");
                            scanf("%d",&choice4);
                            if(choice4!=2){
                            fprintf(fresf,"\n");
                            }
                        }
                    }while(choice3!=2&&choice4!=2);
                    courantf=debf;
                    for(i=1;i<=nbf;i++){
                        courantf=courantf->suivantf;
                    }
                    courantf->suivantf=NULL;
                    free(suivantf);
                }
                printf("stage 21 (1), stage 22 (2) or quit (4)\n");
                scanf("%d",&choice2);
            }
            printf("stage 1 (1), stage 2 (2) or quit (4)\n");
            scanf("%d",&choice1);
            }
        }


    }
Thanks and sorry if the code isn't clear.