21/11/11 14:46
drewnik99
Ho provato a scrivere i singoli elementi di ogni struttura. Questo è il codice:
Restituisce errore nella lettura.
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
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.
Devi salvare in binario tutta la struttura escluso il puntatore.
Devi leggere dal file binario tutta la struttura e ricostruire i puntatori.
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 17:32
drewnik99
Questo è il codice attuale:
C'è ancora qualche problema nella lettura: ho scritto tre strutture collegate in un file; dopo la lettura della seconda il programma restituisce un errore.
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