목록에 파일에서 바로 모드에서 읽기/복사 파일에서 작성하고 올바르게 입력 볼 수 없습니다 :는/내가 읽기에 문제가 있음을했습니다
// 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;
}
이 나를 (예)를 쓰기
을 miarubrica.txt의pippo disney 02345432 [email protected]
하지만리스트 (작동) 판독 방법을 읽을 때, 난 껍질
pippo disney 02345432 [email protected]
pippo disney 02345432 [email protected]
두 번 참조. 뭐가 문제 야? caricalista
에서
첫 번째 반복에서 새 항목은 'p'를 가리 킵니다. 두 번째 반복에서 새 항목은 'p'를 가리 킵니다. 또한 파일이 비어 있으면 함수는'NULL'을 리턴하지만 아마도'p'를 리턴해야합니다. – MRAB
고마워, 나는 그걸 고쳤다. –
Viktor와 MRAB – myself