ho quasi finito ultimo problema la lettura dal file..il programma funziona solo che quando legge il file dopo aver letto tutto si blocca chiudendosi
Codice:
#include<stdio.h>
#include<stdlib.h>
typedef struct autore{
int id;
char nome[20];
char cognome[20];
int brani;
struct autore *next;
}autore;
typedef struct autore a;
typedef a *nodo;
void crea_l(FILE *,autore **);
int stampaINfile(FILE *fpOUT,autore *);
void stampa(nodo );
int main(){
int scelta;
autore *start=NULL;
FILE *fpIN, *fpOUT;
fpIN=fopen("ex02.txt","w"); // creo il file con le liste inserite dall'utente e incremento l'id
crea_l(fpIN,&start);
fclose(fpIN);
printf("La lista.\n");
stampa(start);
fpOUT=fopen("ex02.txt","r");
stampaINfile(fpOUT,start);
printf("FINE.\n");
system("PAUSE");
return 0;
}
void crea_l(FILE *fp,autore **p){
nodo nuovo,corr,prec;
char n[10];
char c[10];
int num,brani,scelta;
int cont=1;
while(scelta!=1){
printf("nome: ");
scanf("%s",n);
printf("cognome: ");
scanf("%s",c);
printf("Brani: ");
scanf("%d",&brani);
nuovo=malloc(sizeof(struct autore));
if(nuovo!=NULL){
nuovo->id=cont++;
strcpy(nuovo->nome,n);
strcpy(nuovo->cognome,c);
nuovo->brani=brani;
nuovo->next=NULL;
prec=NULL;
corr=*p;
while(corr!=NULL && cont > corr->id){
prec=corr;
corr=corr->next;
}
if(prec == NULL){
nuovo->next=*p;
*p=nuovo;
}
else{
prec->next=nuovo;
nuovo->next=corr;
}
fprintf(fp,"**Autore**\n");
fprintf(fp,"Id:%d\n",nuovo->id);
fprintf(fp,"Nome:%s\n",nuovo->nome);
fprintf(fp,"Cognome:%s\n",nuovo->cognome);
fprintf(fp,"Brani:%d\n\n",nuovo->brani);
}
printf("\nFINE? 1=SI 0=N0 ");
scanf("%d",&scelta);
printf("\n");
}
}
void stampa(nodo p){
if(p!= NULL){
printf("\n*** Autore ***\n");
printf("Id: %d\n",p->id);
printf("Nome: %s\n",p->nome);
printf("Cognome: %s\n",p->cognome);
printf("Brani: %d\n\n",p->brani);
stampa(p->next);
}
}
int stampaINfile(FILE *fpOUT,nodo p){
int scan=0;
printf("STAMPA DA FILE\n");
while(!feof(fpOUT)){
fscanf(fpOUT,"%d",&p);
printf("Id: %d\nNome: %s\nCognome: %s\nNumero brani: %d\n\n",p->id,p->nome,p->cognome,p->brani);
}
fclose(fpOUT);
return 0;
}