Anche questa soluzione non funziona, non so perché mi da errore, potreste provare voi per piacere?
codice:#include <stdio.h> #include <string.h> #include <malloc.h> FILE *p; struct alunno { char nome[20]; char cognome[20]; char titolo[30]; struct alunno * succ; }; struct alunno * ins_ord(struct alunno * t, char nome[], char cognome[], char titolo[]){ struct alunno * tmp = t; struct alunno * p; p = (struct alunno *) malloc(sizeof(struct alunno)); if( tmp == NULL || (strcmp(cognome, (*tmp).cognome) == -1)){ strcpy((*p).cognome, cognome); strcpy((*p).nome, nome); strcpy((*p).titolo, titolo); (*p).succ = t; return(p); } else{ while((*tmp).succ != NULL) { tmp = (*tmp).succ; } if(strcmp(cognome, (*tmp).cognome) > 0){ strcpy((*p).cognome, cognome); strcpy((*p).nome, nome); strcpy((*p).titolo, titolo); (*p).succ = NULL; (*tmp).succ = p; return(t); } else{ tmp = t; while(!(strcmp(cognome, (*tmp).cognome) > 0 && strcmp(cognome, (*(*tmp).succ).cognome) < 0)) tmp = (*tmp).succ; strcpy((*p).cognome, cognome); strcpy((*p).nome, nome); strcpy((*p).titolo, titolo); (*p).succ = (*tmp).succ; (*tmp).succ = p; return(t); } } } void salva_file(struct alunno *t){ p = fopen("file.txt", "w"); while(t != NULL){ fprintf(p, "%s ", (*t).cognome); fprintf(p, "%s ", (*t).nome); fprintf(p, "%s \n", (*t).titolo); puts((*t).cognome); t = (*t).succ; } puts("\nDati salvati\n"); fclose(p); } struct alunno * cancella(struct alunno * t){ char cognome[20]; struct alunno* p = t; struct alunno* testa = t; puts("cognome di chi cancellare:"); fflush(stdin); gets(cognome); fflush(stdin); t = (*t).succ; while(t != NULL){ if(strcmp((*t).cognome, cognome) == 0){ (*p).succ = (*t).succ; } p = (*p).succ; t = (*t).succ; } return(testa); } struct alunno* carica() { struct alunno * t; p = fopen("file.txt", "r"); char nome[20]; char cognome[20]; char titolo[30]; fscanf(p, "%s %s %s", cognome, nome, titolo); while (!feof(p)) { t = ins_ord(t, nome, cognome, titolo); fscanf(p, "%s %s %s", nome, cognome, titolo); } fclose(p); return(t); } void main() { int scelta = 9; struct alunno* testa = NULL; char nome [20]; char cognome [20]; char titolo[30]; while(scelta > 0) { puts("1) Carica dati da file\n2) Salva dati su file\n3) Inserisci nuovo prestito\n4) Cancella prestito\n0) Esci\n\n"); scanf("%d", &scelta); if(scelta == 1) { testa = carica(); } if(scelta == 2) { salva_file(testa); } if(scelta == 3) { puts("Inserisci il nome"); fflush(stdin); gets(nome); puts("Inserisci il cognome"); fflush(stdin); gets(cognome); puts("Inserisci il titolo"); fflush(stdin); gets(titolo); testa = ins_ord(testa, nome, cognome, titolo); } if(scelta == 4) { testa = cancella(testa); } } }

Rispondi quotando