Oppure

Loading
19/11/11 14:01
drewnik99
Il mio problema è il seguente: scrivere su files una lista concatenata di strutture per salvare un database per poi in seguito poter aprire quel file, leggere le strutture, ricreare la lista concatenata e operare su di essa.
In che modalità apro il file? Quale funzione uso per scrivere i dati?
Se riuscito postate qualche codice di esempio, grazie.
aaa
19/11/11 16:57
nessuno
Dipende da quanti e quali dati hai nel singolo nodo.

Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
21/11/11 14:46
drewnik99
Ho provato a scrivere i singoli elementi di ogni struttura. Questo è il codice:

struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = 'Ho provato a scrivere i singoli elementi di ogni struttura. Questo è il codice:


struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "r"))
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;
        char v[10];

        fscanf(fr, "%i%*c", nn->code);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fscanf(fr, "%i%*c", &nn->disp);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            fscanf(fr, "%i%*c", &nn->code);

            ch = getc(fr);
            while(ch != '\n' && ch != 1)
            {
                nn->name[i++] = ch;
                ch = getc(fr);
            }
            nn->name[i] = '{parsed_message}';

            ch = getchar();
            while(ch != '\n' && ch != 1)
            {
                nn->des[i++] = ch;
                ch = getc(fr);
            }
            nn->des[i] = '{parsed_message}';

            fscanf(fr, "%i%*c", &nn->disp);

            nn->n = c;
            p->n = nn;
            totc++;
        }
    }
    fclose(fr);
    fflush(NULL);
}

void salva()
{
char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "w"))
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);;
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
    fflush(NULL);
}


Restituisce errore nella lettura.'; totc = 0; if(fr = fopen(path, "r")) { int ch, i = 0; //------------------------------------------------ p = 0; c = comps; char v[10]; fscanf(fr, "%i%*c", nn->code); ch = getc(fr); while(ch != '\n' && ch != 1) { nn->name[i++] = ch; ch = getc(fr); } nn->name[i] = 'Ho provato a scrivere i singoli elementi di ogni struttura. Questo è il codice:

struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "r"))
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;
        char v[10];

        fscanf(fr, "%i%*c", nn->code);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fscanf(fr, "%i%*c", &nn->disp);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            fscanf(fr, "%i%*c", &nn->code);

            ch = getc(fr);
            while(ch != '\n' && ch != 1)
            {
                nn->name[i++] = ch;
                ch = getc(fr);
            }
            nn->name[i] = '{parsed_message}';

            ch = getchar();
            while(ch != '\n' && ch != 1)
            {
                nn->des[i++] = ch;
                ch = getc(fr);
            }
            nn->des[i] = '{parsed_message}';

            fscanf(fr, "%i%*c", &nn->disp);

            nn->n = c;
            p->n = nn;
            totc++;
        }
    }
    fclose(fr);
    fflush(NULL);
}

void salva()
{
char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "w"))
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);;
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
    fflush(NULL);
}


Restituisce errore nella lettura.'; ch = getc(fr); while(ch != '\n' && ch != 1) { nn->des[i++] = ch; ch = getc(fr); } nn->des[i] = 'Ho provato a scrivere i singoli elementi di ogni struttura. Questo è il codice:

struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "r"))
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;
        char v[10];

        fscanf(fr, "%i%*c", nn->code);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fscanf(fr, "%i%*c", &nn->disp);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            fscanf(fr, "%i%*c", &nn->code);

            ch = getc(fr);
            while(ch != '\n' && ch != 1)
            {
                nn->name[i++] = ch;
                ch = getc(fr);
            }
            nn->name[i] = '{parsed_message}';

            ch = getchar();
            while(ch != '\n' && ch != 1)
            {
                nn->des[i++] = ch;
                ch = getc(fr);
            }
            nn->des[i] = '{parsed_message}';

            fscanf(fr, "%i%*c", &nn->disp);

            nn->n = c;
            p->n = nn;
            totc++;
        }
    }
    fclose(fr);
    fflush(NULL);
}

void salva()
{
char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "w"))
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);;
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
    fflush(NULL);
}


Restituisce errore nella lettura.'; fscanf(fr, "%i%*c", &nn->disp); nn->n = c; comps = nn; totc++; //------------------------------------------------ while(feof(fr) == 0) { fscanf(fr, "%i%*c", &nn->code); ch = getc(fr); while(ch != '\n' && ch != 1) { nn->name[i++] = ch; ch = getc(fr); } nn->name[i] = 'Ho provato a scrivere i singoli elementi di ogni struttura. Questo è il codice:

struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "r"))
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;
        char v[10];

        fscanf(fr, "%i%*c", nn->code);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fscanf(fr, "%i%*c", &nn->disp);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            fscanf(fr, "%i%*c", &nn->code);

            ch = getc(fr);
            while(ch != '\n' && ch != 1)
            {
                nn->name[i++] = ch;
                ch = getc(fr);
            }
            nn->name[i] = '{parsed_message}';

            ch = getchar();
            while(ch != '\n' && ch != 1)
            {
                nn->des[i++] = ch;
                ch = getc(fr);
            }
            nn->des[i] = '{parsed_message}';

            fscanf(fr, "%i%*c", &nn->disp);

            nn->n = c;
            p->n = nn;
            totc++;
        }
    }
    fclose(fr);
    fflush(NULL);
}

void salva()
{
char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "w"))
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);;
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
    fflush(NULL);
}


Restituisce errore nella lettura.'; ch = getchar(); while(ch != '\n' && ch != 1) { nn->des[i++] = ch; ch = getc(fr); } nn->des[i] = 'Ho provato a scrivere i singoli elementi di ogni struttura. Questo è il codice:

struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "r"))
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;
        char v[10];

        fscanf(fr, "%i%*c", nn->code);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fscanf(fr, "%i%*c", &nn->disp);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            fscanf(fr, "%i%*c", &nn->code);

            ch = getc(fr);
            while(ch != '\n' && ch != 1)
            {
                nn->name[i++] = ch;
                ch = getc(fr);
            }
            nn->name[i] = '{parsed_message}';

            ch = getchar();
            while(ch != '\n' && ch != 1)
            {
                nn->des[i++] = ch;
                ch = getc(fr);
            }
            nn->des[i] = '{parsed_message}';

            fscanf(fr, "%i%*c", &nn->disp);

            nn->n = c;
            p->n = nn;
            totc++;
        }
    }
    fclose(fr);
    fflush(NULL);
}

void salva()
{
char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "w"))
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);;
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
    fflush(NULL);
}


Restituisce errore nella lettura.'; fscanf(fr, "%i%*c", &nn->disp); nn->n = c; p->n = nn; totc++; } } fclose(fr); fflush(NULL); } void salva() { char *path = malloc(sizeof(char) * 50); int ch, i = 0; FILE *fw; struct comp *p; fprintf(stderr, "\nInserisci il percorso e il nome del file: "); fflush(stdin); while((ch = getc(stdin)) != '\n' && i < 256) { path[i++] = ch; } path[i] = 'Ho provato a scrivere i singoli elementi di ogni struttura. Questo è il codice:

struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "r"))
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;
        char v[10];

        fscanf(fr, "%i%*c", nn->code);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fscanf(fr, "%i%*c", &nn->disp);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            fscanf(fr, "%i%*c", &nn->code);

            ch = getc(fr);
            while(ch != '\n' && ch != 1)
            {
                nn->name[i++] = ch;
                ch = getc(fr);
            }
            nn->name[i] = '{parsed_message}';

            ch = getchar();
            while(ch != '\n' && ch != 1)
            {
                nn->des[i++] = ch;
                ch = getc(fr);
            }
            nn->des[i] = '{parsed_message}';

            fscanf(fr, "%i%*c", &nn->disp);

            nn->n = c;
            p->n = nn;
            totc++;
        }
    }
    fclose(fr);
    fflush(NULL);
}

void salva()
{
char *path = malloc(sizeof(char) * 50);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "w"))
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);;
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
    fflush(NULL);
}


Restituisce errore nella lettura.'; if(fw = fopen(path, "w")) { for(p = comps; p; p = p->n) { fwrite(&p->code, sizeof(int), 1, fw); fputc(1, fw); fwrite(p->name, sizeof(char), strlen(p->name), fw); fputc(1, fw);; fwrite(p->des, sizeof(char), strlen(p->des), fw); fputc(1, fw); fwrite(&p->disp, sizeof(int), 1, fw); fputc('\n', fw); } } fclose(fw); fflush(NULL); }


Restituisce errore nella lettura.
Ultima modifica effettuata da drewnik99 21/11/11 14:48
aaa
21/11/11 15:20
nessuno
E' tutto sbagliato logicamente ...

Devi salvare in binario tutta la struttura escluso il puntatore.

Devi leggere dal file binario tutta la struttura e ricostruire i puntatori.
Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
21/11/11 15:42
drewnik99
Ho deciso di non salvare la struttura completa, perchè non è detto che name e des siano sempre lunghe rispettivamente 25 e 100, quindi posso risparmiare spazio.
aaa
21/11/11 16:43
nessuno
Allora salva in un file di testo, non in un file binario.

Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
21/11/11 17:32
drewnik99
Questo è il codice attuale:


struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn = calloc(1, sizeof(struct comp));

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = 'Questo è il codice attuale:



struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn = calloc(1, sizeof(struct comp));

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "rb") != NULL)
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            nn = calloc(1, sizeof(struct comp));

            fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        p->n = nn;
        totc++;
        }
    }
    fclose(fr);
}

void salva()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "wb") != NULL)
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
}


C'è ancora qualche problema nella lettura: ho scritto tre strutture collegate in un file; dopo la lettura della seconda il programma restituisce un errore.'; totc = 0; if(fr = fopen(path, "rb") != NULL) { int ch, i = 0; //------------------------------------------------ p = 0; c = comps; fread(&nn->code, sizeof(int), 1, fr); getc(fr); ch = getc(fr); while(ch != '\n' && ch != 1) { nn->name[i++] = ch; ch = getc(fr); } nn->name[i] = 'Questo è il codice attuale:


struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn = calloc(1, sizeof(struct comp));

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "rb") != NULL)
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            nn = calloc(1, sizeof(struct comp));

            fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        p->n = nn;
        totc++;
        }
    }
    fclose(fr);
}

void salva()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "wb") != NULL)
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
}


C'è ancora qualche problema nella lettura: ho scritto tre strutture collegate in un file; dopo la lettura della seconda il programma restituisce un errore.'; i = 0; ch = getc(fr); while(ch != '\n' && ch != 1) { nn->des[i++] = ch; ch = getc(fr); } nn->des[i] = 'Questo è il codice attuale:


struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn = calloc(1, sizeof(struct comp));

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "rb") != NULL)
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            nn = calloc(1, sizeof(struct comp));

            fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        p->n = nn;
        totc++;
        }
    }
    fclose(fr);
}

void salva()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "wb") != NULL)
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
}


C'è ancora qualche problema nella lettura: ho scritto tre strutture collegate in un file; dopo la lettura della seconda il programma restituisce un errore.'; fread(&nn->code, sizeof(int), 1, fr); getc(fr); nn->n = c; comps = nn; totc++; //------------------------------------------------ while(feof(fr) == 0) { nn = calloc(1, sizeof(struct comp)); fread(&nn->code, sizeof(int), 1, fr); getc(fr); ch = getc(fr); while(ch != '\n' && ch != 1) { nn->name[i++] = ch; ch = getc(fr); } nn->name[i] = 'Questo è il codice attuale:


struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn = calloc(1, sizeof(struct comp));

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "rb") != NULL)
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            nn = calloc(1, sizeof(struct comp));

            fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        p->n = nn;
        totc++;
        }
    }
    fclose(fr);
}

void salva()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "wb") != NULL)
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
}


C'è ancora qualche problema nella lettura: ho scritto tre strutture collegate in un file; dopo la lettura della seconda il programma restituisce un errore.'; i = 0; ch = getc(fr); while(ch != '\n' && ch != 1) { nn->des[i++] = ch; ch = getc(fr); } nn->des[i] = 'Questo è il codice attuale:


struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn = calloc(1, sizeof(struct comp));

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "rb") != NULL)
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            nn = calloc(1, sizeof(struct comp));

            fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        p->n = nn;
        totc++;
        }
    }
    fclose(fr);
}

void salva()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "wb") != NULL)
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
}


C'è ancora qualche problema nella lettura: ho scritto tre strutture collegate in un file; dopo la lettura della seconda il programma restituisce un errore.'; fread(&nn->code, sizeof(int), 1, fr); getc(fr); nn->n = c; p->n = nn; totc++; } } fclose(fr); } void salva() { char *path = malloc(sizeof(char) * 256); int ch, i = 0; FILE *fw; struct comp *p; fprintf(stderr, "\nInserisci il percorso e il nome del file: "); fflush(stdin); while((ch = getc(stdin)) != '\n' && i < 256) { path[i++] = ch; } path[i] = 'Questo è il codice attuale:


struct comp
{
    int code;
    char name[25];
    char des[100];
    int disp;
    struct comp *n;
};

struct comp *comps = 0;
int totc = 0;

void apri()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fr;
    struct comp *p, *c, *nn = calloc(1, sizeof(struct comp));

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    totc = 0;
    if(fr = fopen(path, "rb") != NULL)
    {
        int ch, i = 0;

        //------------------------------------------------
        p = 0;
        c = comps;

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        comps = nn;
        totc++;
        //------------------------------------------------

        while(feof(fr) == 0)
        {
            nn = calloc(1, sizeof(struct comp));

            fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->name[i++] = ch;
            ch = getc(fr);
        }
        nn->name[i] = '{parsed_message}';

        i = 0;
        ch = getc(fr);
        while(ch != '\n' && ch != 1)
        {
            nn->des[i++] = ch;
            ch = getc(fr);
        }
        nn->des[i] = '{parsed_message}';

        fread(&nn->code, sizeof(int), 1, fr);

        getc(fr);

        nn->n = c;
        p->n = nn;
        totc++;
        }
    }
    fclose(fr);
}

void salva()
{
    char *path = malloc(sizeof(char) * 256);
    int ch, i = 0;
    FILE *fw;
    struct comp *p;

    fprintf(stderr, "\nInserisci il percorso e il nome del file: ");

    fflush(stdin);
    while((ch = getc(stdin)) != '\n' && i < 256)
    {
        path[i++] = ch;
    }
    path[i] = '{parsed_message}';

    if(fw = fopen(path, "wb") != NULL)
    {
        for(p = comps; p; p = p->n)
        {
            fwrite(&p->code, sizeof(int), 1, fw);
            fputc(1, fw);
            fwrite(p->name, sizeof(char), strlen(p->name), fw);
            fputc(1, fw);
            fwrite(p->des, sizeof(char), strlen(p->des), fw);
            fputc(1, fw);
            fwrite(&p->disp, sizeof(int), 1, fw);
            fputc('\n', fw);
        }
    }
    fclose(fw);
}


C'è ancora qualche problema nella lettura: ho scritto tre strutture collegate in un file; dopo la lettura della seconda il programma restituisce un errore.'; if(fw = fopen(path, "wb") != NULL) { for(p = comps; p; p = p->n) { fwrite(&p->code, sizeof(int), 1, fw); fputc(1, fw); fwrite(p->name, sizeof(char), strlen(p->name), fw); fputc(1, fw); fwrite(p->des, sizeof(char), strlen(p->des), fw); fputc(1, fw); fwrite(&p->disp, sizeof(int), 1, fw); fputc('\n', fw); } } fclose(fw); }


C'è ancora qualche problema nella lettura: ho scritto tre strutture collegate in un file; dopo la lettura della seconda il programma restituisce un errore.
Ultima modifica effettuata da drewnik99 21/11/11 17:35
aaa
22/11/11 9:26
nessuno
Se insisti a farlo in binario (e non in modalità testo come ti avevo suggerito) non so cosa dirti. Anche perché quando dici "restituisce un errore" ce lo dobbiamo inventare noi quale errore? Perché non lo dici?
Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.