无法以正确模式从文件复制/读取到列表

时间:2012-07-13 16:36:19

标签: c list addressbook scanf

我从文件读取/写入时出现问题并正确查看输入:

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

在shell中两次。怎么了?

2 个答案:

答案 0 :(得分:2)

caricalista中,似乎有一些混淆,关于你是否在前置(将新条目放在 p指向的那个之前)或追加(放入在 p指向的之后的新条目

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

此外,如果文件为空,则会返回punt未初始化。

答案 1 :(得分:2)

这是一个快速修复。你用“ - >双关语”指针混合了这些东西。我已经删除了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; }