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;
}

这写给我(例子)

pippo disney 02345432 pippodisney@pippodisney.com  

在 miarubrica.txt 但是当我使用读取列表的方法读取它时(它有效),我看到了

pippo disney 02345432 pippodisney@pippodisney.com
pippo disney 02345432 pippodisney@pippodisney.com

在壳中两次。怎么了?

4

2 回答 2

2

对于您是前置(将新条目放在by 指向的条目之前)还是附加(将新条目放在by指向的条目之后caricalista) ,似乎存在一些混淆。pp

例如,如果pis not NULL,它会punt->pun = p;,保持p不变,但在下一次迭代中它会做同样的事情。

此外,如果文件为空,它将返回punt未初始化的。

于 2012-07-13T17:13:19.433 回答
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; }
于 2012-07-13T17:19:43.577 回答