Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Scrivere su files una lista concatenata di strutture
Forum - C/C++ - Scrivere su files una lista concatenata di strutture

Avatar
drewnik99 (Normal User)
Pro


Messaggi: 69
Iscritto: 28/03/2008

Segnala al moderatore
Postato alle 15:01
Sabato, 19/11/2011
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.

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 17:57
Sabato, 19/11/2011
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à.
PM Quote
Avatar
drewnik99 (Normal User)
Pro


Messaggi: 69
Iscritto: 28/03/2008

Segnala al moderatore
Postato alle 15:46
Lunedì, 21/11/2011
Ho provato a scrivere i singoli elementi di ogni struttura. Questo è il codice:

Codice sorgente - presumibilmente C++

  1. struct comp
  2. {
  3.     int code;
  4.     char name[25];
  5.     char des[100];
  6.     int disp;
  7.     struct comp *n;
  8. };
  9.  
  10. struct comp *comps = 0;
  11. int totc = 0;
  12.  
  13. void apri()
  14. {
  15.     char *path = malloc(sizeof(char) * 50);
  16.     int ch, i = 0;
  17.     FILE *fr;
  18.     struct comp *p, *c, *nn;
  19.  
  20.     fprintf(stderr, "\nInserisci il percorso e il nome del file: ");
  21.  
  22.     fflush(stdin);
  23.     while((ch = getc(stdin)) != '\n' && i < 256)
  24.     {
  25.         path[i++] = ch;
  26.     }
  27.     path[i] = '\0';
  28.  
  29.     totc = 0;
  30.     if(fr = fopen(path, "r"))
  31.     {
  32.         int ch, i = 0;
  33.  
  34.         //------------------------------------------------
  35.         p = 0;
  36.         c = comps;
  37.         char v[10];
  38.  
  39.         fscanf(fr, "%i%*c", nn->code);
  40.  
  41.         ch = getc(fr);
  42.         while(ch != '\n' && ch != 1)
  43.         {
  44.             nn->name[i++] = ch;
  45.             ch = getc(fr);
  46.         }
  47.         nn->name[i] = '\0';
  48.  
  49.         ch = getc(fr);
  50.         while(ch != '\n' && ch != 1)
  51.         {
  52.             nn->des[i++] = ch;
  53.             ch = getc(fr);
  54.         }
  55.         nn->des[i] = '\0';
  56.  
  57.         fscanf(fr, "%i%*c", &nn->disp);
  58.  
  59.         nn->n = c;
  60.         comps = nn;
  61.         totc++;
  62.         //------------------------------------------------
  63.  
  64.         while(feof(fr) == 0)
  65.         {
  66.             fscanf(fr, "%i%*c", &nn->code);
  67.  
  68.             ch = getc(fr);
  69.             while(ch != '\n' && ch != 1)
  70.             {
  71.                 nn->name[i++] = ch;
  72.                 ch = getc(fr);
  73.             }
  74.             nn->name[i] = '\0';
  75.  
  76.             ch = getchar();
  77.             while(ch != '\n' && ch != 1)
  78.             {
  79.                 nn->des[i++] = ch;
  80.                 ch = getc(fr);
  81.             }
  82.             nn->des[i] = '\0';
  83.  
  84.             fscanf(fr, "%i%*c", &nn->disp);
  85.  
  86.             nn->n = c;
  87.             p->n = nn;
  88.             totc++;
  89.         }
  90.     }
  91.     fclose(fr);
  92.     fflush(NULL);
  93. }
  94.  
  95. void salva()
  96. {
  97. char *path = malloc(sizeof(char) * 50);
  98.     int ch, i = 0;
  99.     FILE *fw;
  100.     struct comp *p;
  101.  
  102.     fprintf(stderr, "\nInserisci il percorso e il nome del file: ");
  103.  
  104.     fflush(stdin);
  105.     while((ch = getc(stdin)) != '\n' && i < 256)
  106.     {
  107.         path[i++] = ch;
  108.     }
  109.     path[i] = '\0';
  110.  
  111.     if(fw = fopen(path, "w"))
  112.     {
  113.         for(p = comps; p; p = p->n)
  114.         {
  115.             fwrite(&p->code, sizeof(int), 1, fw);
  116.             fputc(1, fw);
  117.             fwrite(p->name, sizeof(char), strlen(p->name), fw);
  118.             fputc(1, fw);;
  119.             fwrite(p->des, sizeof(char), strlen(p->des), fw);
  120.             fputc(1, fw);
  121.             fwrite(&p->disp, sizeof(int), 1, fw);
  122.             fputc('\n', fw);
  123.         }
  124.     }
  125.     fclose(fw);
  126.     fflush(NULL);
  127. }



Restituisce errore nella lettura.

Ultima modifica effettuata da drewnik99 il 21/11/2011 alle 15:48
PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 16:20
Lunedì, 21/11/2011
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à.
PM Quote
Avatar
drewnik99 (Normal User)
Pro


Messaggi: 69
Iscritto: 28/03/2008

Segnala al moderatore
Postato alle 16:42
Lunedì, 21/11/2011
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.

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 17:43
Lunedì, 21/11/2011
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à.
PM Quote
Avatar
drewnik99 (Normal User)
Pro


Messaggi: 69
Iscritto: 28/03/2008

Segnala al moderatore
Postato alle 18:32
Lunedì, 21/11/2011
Questo è il codice attuale:

Codice sorgente - presumibilmente C++

  1. struct comp
  2. {
  3.     int code;
  4.     char name[25];
  5.     char des[100];
  6.     int disp;
  7.     struct comp *n;
  8. };
  9.  
  10. struct comp *comps = 0;
  11. int totc = 0;
  12.  
  13. void apri()
  14. {
  15.     char *path = malloc(sizeof(char) * 256);
  16.     int ch, i = 0;
  17.     FILE *fr;
  18.     struct comp *p, *c, *nn = calloc(1, sizeof(struct comp));
  19.  
  20.     fprintf(stderr, "\nInserisci il percorso e il nome del file: ");
  21.  
  22.     fflush(stdin);
  23.     while((ch = getc(stdin)) != '\n' && i < 256)
  24.     {
  25.         path[i++] = ch;
  26.     }
  27.     path[i] = '\0';
  28.  
  29.     totc = 0;
  30.     if(fr = fopen(path, "rb") != NULL)
  31.     {
  32.         int ch, i = 0;
  33.  
  34.         //------------------------------------------------
  35.         p = 0;
  36.         c = comps;
  37.  
  38.         fread(&nn->code, sizeof(int), 1, fr);
  39.  
  40.         getc(fr);
  41.  
  42.         ch = getc(fr);
  43.         while(ch != '\n' && ch != 1)
  44.         {
  45.             nn->name[i++] = ch;
  46.             ch = getc(fr);
  47.         }
  48.         nn->name[i] = '\0';
  49.  
  50.         i = 0;
  51.         ch = getc(fr);
  52.         while(ch != '\n' && ch != 1)
  53.         {
  54.             nn->des[i++] = ch;
  55.             ch = getc(fr);
  56.         }
  57.         nn->des[i] = '\0';
  58.  
  59.         fread(&nn->code, sizeof(int), 1, fr);
  60.  
  61.         getc(fr);
  62.  
  63.         nn->n = c;
  64.         comps = nn;
  65.         totc++;
  66.         //------------------------------------------------
  67.  
  68.         while(feof(fr) == 0)
  69.         {
  70.             nn = calloc(1, sizeof(struct comp));
  71.  
  72.             fread(&nn->code, sizeof(int), 1, fr);
  73.  
  74.         getc(fr);
  75.  
  76.         ch = getc(fr);
  77.         while(ch != '\n' && ch != 1)
  78.         {
  79.             nn->name[i++] = ch;
  80.             ch = getc(fr);
  81.         }
  82.         nn->name[i] = '\0';
  83.  
  84.         i = 0;
  85.         ch = getc(fr);
  86.         while(ch != '\n' && ch != 1)
  87.         {
  88.             nn->des[i++] = ch;
  89.             ch = getc(fr);
  90.         }
  91.         nn->des[i] = '\0';
  92.  
  93.         fread(&nn->code, sizeof(int), 1, fr);
  94.  
  95.         getc(fr);
  96.  
  97.         nn->n = c;
  98.         p->n = nn;
  99.         totc++;
  100.         }
  101.     }
  102.     fclose(fr);
  103. }
  104.  
  105. void salva()
  106. {
  107.     char *path = malloc(sizeof(char) * 256);
  108.     int ch, i = 0;
  109.     FILE *fw;
  110.     struct comp *p;
  111.  
  112.     fprintf(stderr, "\nInserisci il percorso e il nome del file: ");
  113.  
  114.     fflush(stdin);
  115.     while((ch = getc(stdin)) != '\n' && i < 256)
  116.     {
  117.         path[i++] = ch;
  118.     }
  119.     path[i] = '\0';
  120.  
  121.     if(fw = fopen(path, "wb") != NULL)
  122.     {
  123.         for(p = comps; p; p = p->n)
  124.         {
  125.             fwrite(&p->code, sizeof(int), 1, fw);
  126.             fputc(1, fw);
  127.             fwrite(p->name, sizeof(char), strlen(p->name), fw);
  128.             fputc(1, fw);
  129.             fwrite(p->des, sizeof(char), strlen(p->des), fw);
  130.             fputc(1, fw);
  131.             fwrite(&p->disp, sizeof(int), 1, fw);
  132.             fputc('\n', fw);
  133.         }
  134.     }
  135.     fclose(fw);
  136. }



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 il 21/11/2011 alle 18:35
PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 10:26
Martedì, 22/11/2011
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à.
PM Quote
Avatar
drewnik99 (Normal User)
Pro


Messaggi: 69
Iscritto: 28/03/2008

Segnala al moderatore
Postato alle 19:45
Martedì, 22/11/2011
Ho risolto. Ecco il codice completo:

Codice sorgente - presumibilmente C++

  1. struct comp
  2. {
  3.     int code;
  4.     char name[27];
  5.     char des[102];
  6.     int disp;
  7.     struct comp *n;
  8. };
  9.  
  10. struct comp *comps = 0;
  11.  
  12. void apri()
  13. {
  14.     char *path = malloc(sizeof(char) * 256);
  15.     int ch, i = 0;
  16.     FILE *fr;
  17.     struct comp *nn = calloc(1, sizeof(struct comp)), *p = 0;
  18.  
  19.     fputs("\nInserisci il percorso e il nome del file: ", stderr);
  20.  
  21.     fflush(stdin);
  22.     while((ch = getc(stdin)) != '\n' && i < 256)
  23.     {
  24.         path[i++] = ch;
  25.     }
  26.     path[i] = '\0';
  27.  
  28.     if((fr = fopen(path, "rb")) != NULL)
  29.     {
  30.         comps = nn;
  31.  
  32.         //-----------------------------------------------------------------   prima struttura
  33.         fread(&nn->code, sizeof(int), 1, fr);
  34.  
  35.         i = 0;
  36.         ch = getc(fr);
  37.         do
  38.         {
  39.             nn->name[i++] = ch;
  40.             ch = getc(fr);
  41.         }
  42.         while(ch != '\0');
  43.  
  44.         i = 0;
  45.         ch = getc(fr);
  46.         do
  47.         {
  48.             nn->des[i++] = ch;
  49.             ch = getc(fr);
  50.         }
  51.         while(ch != '\0');
  52.  
  53.         fread(&nn->disp, sizeof(int), 1, fr);
  54.  
  55.         getc(fr);
  56.  
  57.         p = nn;
  58.         nn = calloc(1, sizeof(struct comp));
  59.         //-----------------------------------------------------------------   fine prima struttura
  60.  
  61.         while((ch = getc(fr)) && (!feof(fr)) && ungetc(ch, fr))
  62.         {
  63.             fread(&nn->code, sizeof(int), 1, fr);
  64.  
  65.             i = 0;
  66.             ch = getc(fr);
  67.             do
  68.             {
  69.                 nn->name[i++] = ch;
  70.                 ch = getc(fr);
  71.             }
  72.             while(ch != '\0');
  73.  
  74.             i = 0;
  75.             ch = getc(fr);
  76.             do
  77.             {
  78.                 nn->des[i++] = ch;
  79.                 ch = getc(fr);
  80.             }
  81.             while(ch != '\0');
  82.  
  83.             fread(&nn->disp, sizeof(int), 1, fr);
  84.  
  85.             getc(fr);
  86.  
  87.             p->n = nn;
  88.             p = nn;
  89.             nn = calloc(1, sizeof(struct comp));
  90.         }
  91.         p->n = 0;
  92.     }
  93.  
  94.     fclose(fr);
  95.     free(path);
  96.     free(nn);
  97. }
  98.  
  99. void salva()
  100. {
  101.     char *path = malloc(sizeof(char) * 256);
  102.     int ch, i = 0;
  103.     FILE *fw;
  104.     struct comp *p;
  105.  
  106.     fputs("\nInserisci il percorso e il nome del file: ", stderr);
  107.  
  108.     fflush(stdin);
  109.     while((ch = getc(stdin)) != '\n' && i < 256)
  110.     {
  111.         path[i++] = ch;
  112.     }
  113.     path[i] = '\0';
  114.  
  115.     if((fw = fopen(path, "wb")) != NULL)
  116.     {
  117.         for(p = comps; p; p = p->n)
  118.         {
  119.             fwrite(&p->code, sizeof(int), 1, fw);
  120.             fwrite(p->name, sizeof(char), strlen(p->name), fw);
  121.             fputc('\0', fw);
  122.             fwrite(p->des, sizeof(char), strlen(p->des), fw);
  123.             fputc('\0', fw);
  124.             fwrite(&p->disp, sizeof(int), 1, fw);
  125.             fputc('\n', fw);
  126.         }
  127.     }
  128.  
  129.     fclose(fw);
  130.     free(path);
  131. }



Grazie a tutti per il vostro aiuto.

Ultima modifica effettuata da drewnik99 il 22/11/2011 alle 19:47
PM Quote