我从文件读/写有问题并正确查看输入:
// LOAD THE LIST FROM THE FILE
struct elemento *caricalista(struct elemento *p) {
struct elemento *punt;
FILE * utenti = fopen ("miarubrica.txt", "r");
char nome[MAX];
char cognome[MAX];
char telefono[MAX];
char mail[MAX];
if (utenti == NULL) {
printf("non ho caricato gli utenti");
} else {
while (!feof(utenti)) {
if (p != NULL) {
punt = (struct elemento *)malloc(sizeof(struct elemento));
fscanf(utenti, "%s", nome);
puts(nome);
fscanf(utenti, "%s", cognome);
puts(cognome);
fscanf(utenti, "%s", telefono);
puts(telefono);
fscanf(utenti, "%s", mail);
puts(mail);
strcpy(punt->nome, nome);
strcpy(punt->cognome, cognome);
strcpy(punt->telefono, telefono);
strcpy(punt->mail, mail);
punt->pun = p;
} else if (p == NULL) {
p = (struct elemento *)malloc(sizeof(struct elemento));
fscanf(utenti, "%s", nome);
fscanf(utenti, "%s", cognome);
fscanf(utenti, "%s", telefono);
fscanf(utenti, "%s", mail);
strcpy(p->nome, nome);
strcpy(p->cognome, cognome);
strcpy(p->telefono, telefono);
strcpy(p->mail, mail);
p->pun = NULL;
punt = p;
}
}
}
fflush(utenti);
fclose(utenti);
return(punt);
}
// SAVE THE LIST
int salva(struct elemento *p) {
FILE *stream = fopen("miarubrica.txt", "w");
while (p != NULL) {
// Scrive sul file
fprintf(stream, "%s ", p->nome);
fprintf(stream, "%s ", p->cognome);
fprintf(stream, "%s ", p->mail);
fprintf(stream, "%s \n", p->telefono);
p = p->pun;
}
fflush(stream);
fclose(stream);
return;
}
这写给我(例子)
pippo disney 02345432 pippodisney@pippodisney.com
在 miarubrica.txt 但是当我使用读取列表的方法读取它时(它有效),我看到了
pippo disney 02345432 pippodisney@pippodisney.com
pippo disney 02345432 pippodisney@pippodisney.com
在壳中两次。怎么了?