codice:
#include <stdio.h>
struct elemento{
char nome[50];
int n;
struct elemento *next;
};
struct elemento *crealista(){
struct elemento *p;
char c;
char k[50];
printf("vuoi aggiungere un elemento? permi 's' per si 'n' per no e stampare\n");
scanf("%c",&c);
if(c=='n'){
p=NULL;
}
else if(c=='s'){
while(c=='s'){
p=(struct elemento*)malloc(sizeof(struct elemento));
printf("inserisci nome : ");
scanf("%s",k);
strcpy(p->nome,k);
printf("inserisci numero : ");
scanf("%d",&p->n);
scanf("%c",&c);
if(c=='n')
p->next=NULL;
else
p=crealista();
}
}
return (p);
}
void stampa(struct elemento *p){
printf( " Lista --> ");
while(p!= NULL){
printf("%s", p->nome);
printf("-->");
printf("%d",p->n);
printf("-->");
p=p->next;
}
printf(" fine \n ");
}
void *creafile(struct elemento *p){
FILE *f;
f=fopen("rubrica2.txt","w");
if(f==NULL)
printf("ERRORE");
else
while(p!=NULL){
fprintf(f,"%s",p->nome);
fprintf(f,"%d",p->n);
p=p->next;
}
fclose(f);
}
main(){
struct elemento *e;
e=crealista();
stampa(e);
creafile(e);
system("PAUSE");
}
è lo stesso programma di prima però con struttura diversa, ho solo aggiunto alla struttura delle stringhe, ma stavolta non riesco ne a stampare su schermo ne a stampare su file! com'è possibile?