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.