- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
node_t* read_record(FILE *file) {
int success = 0;
node_t *record = 0;
do {
int nsz, dsz;
record = (node_t*)malloc(sizeof(node_t));
if (!record) {
break;
}
record->data = 0;
record->next = 0;
if (fread(&nsz, sizeof(int), 1, file) != 1 || feof(file)) {
break;
}
if (fread(record->name, 1, nsz, file) != nsz || feof(file)) {
break;
}
/* ... */
record->nsz = nsz;
record->dsz = dsz;
success = 1;
} while (0);
if (record && !success) {
free_list(record);
record = 0;
}
return record;
}
void write_record(FILE *file, node_t *record) {
int dsz = record->dsz;
int nsz = record->nsz;
if ((fwrite(&nsz, sizeof(int), 1, file) != 1) ||
(fwrite(record->name, 1, nsz, file) != nsz) ||
(fwrite(&csz, sizeof(int), 1, file) != 1) ||
(fwrite(record->data, 1, dsz, file) != dsz) ||
ferror(file)) {
fputs("Error: write_record", stderr);
}
}