2012-07-13 3 views
2

목록에 파일에서 바로 모드에서 읽기/복사 파일에서 작성하고 올바르게 입력 볼 수 없습니다 :는/내가 읽기에 문제가 있음을했습니다

// 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에서

답변

2

다음은 빠른 해결 방법입니다. "-> pun"포인터로 내용을 혼합했습니다. salva() 메서드는 사용하지 않으므로 제거했습니다.

#include <stdio.h> 
#include <malloc.h> 

#define MAX (256) 
struct elemento { 
    char nome[MAX], cognome[MAX], telefono[MAX], mail[MAX]; 
    struct elemento* pun; 
}; 

// LOAD THE LIST FROM THE FILE 
struct elemento *caricalista(struct elemento *p) { 
    struct elemento *punt = p; 
    FILE * utenti = fopen ("miarubrica.txt","r"); 

    if(!utenti) { printf("non ho caricato gli utenti"); return p; } 

    while(!feof(utenti)) {  
     punt= (struct elemento *)malloc(sizeof(struct elemento)); 

     fscanf(utenti,"%s%s%s%s", 
       punt->nome, punt->cognome, punt->telefono, punt->mail); 

     printf("%s %s %s %s\n", /* print new element */ 
      punt->nome, punt->cognome, punt->telefono, punt->mail); 

     punt->pun = p; /* old list at the end */ 
     p = punt; 
    } 

    fclose(utenti); 
    return(punt); 
} 

int main() { caricalista(NULL); return 0; } 
+0

첫 번째 반복에서 새 항목은 'p'를 가리 킵니다. 두 번째 반복에서 새 항목은 'p'를 가리 킵니다. 또한 파일이 비어 있으면 함수는'NULL'을 리턴하지만 아마도'p'를 리턴해야합니다. – MRAB

+0

고마워, 나는 그걸 고쳤다. –

+0

Viktor와 MRAB – myself

2

당신이 (하나는 p가 가리키는 전에 새 항목 을 가하고) 앞에 추가 또는 추가하고 있는지에 대해 약간의 혼동이있을 나타납니다 ( 후 새 항목 을 넣어 하나 p가 가리키는) .

pNULL가없는 경우 예를 들어,이 p이 변경되지 떠나, punt->pun = p; 않습니다, 그러나 다음 반복에 동일한 작업을 수행합니다.

또한 파일이 비어 있으면 punt은 초기화되지 않은 파일을 반환합니다.

관련 문제