2

i've a problem with read/write from file and view correctly the input:

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

this write me (example)

pippo disney 02345432 pippodisney@pippodisney.com  

in miarubrica.txt but when i read it with a method that read lists (it works), i see

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

two times in the shell. what's wrong?

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
myself
  • 89
  • 1
  • 8

2 Answers2

2

In caricalista there appears to be some confusion about whether you're prepending (putting the new entry before the one pointed to by p) or appending (put the new entry after the one pointed to by p).

For example, if p isn't NULL, it does punt->pun = p;, leaving p unchanged, but then on the next iteration it does the same.

Also, if the file is empty, it will return punt uninitialised.

MRAB
  • 20,356
  • 6
  • 40
  • 33
2

Here's a quick fix. You've mixed the stuff with "->pun" pointer. I've removed the salva() method since you do not use it.

#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; }
Viktor Latypov
  • 14,289
  • 3
  • 40
  • 55
  • On the first iteration the new entry will point to `p`. On the second iteration the new entry will also point to `p`. Also, if the file is empty, the function will return `NULL`, but perhaps it should return `p` instead. – MRAB Jul 13 '12 at 17:26
  • It should still return `p`. After all, if the file doesn't exist then it returns `p`. – MRAB Jul 13 '12 at 18:16
  • Here's the line: if(!utenti) { printf("non ho caricato gli utenti"); return p; } – Viktor Latypov Jul 13 '12 at 18:55
  • It returns p, I've just compressed the source to the single line to make things shorter. I've already told that I appreciate your comment about "return p". – Viktor Latypov Jul 13 '12 at 18:56
  • I'm talking about the `return(punt);` at the end of the function. – MRAB Jul 13 '12 at 19:12
  • Oh, I see. Then we might just initialize the 'punt' with 'p' at the beginning. If the file is empty, punt will be returned. – Viktor Latypov Jul 13 '12 at 19:15