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