-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-34
More file actions
41 lines (33 loc) · 802 Bytes
/
problem-34
File metadata and controls
41 lines (33 loc) · 802 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int id;
char data[256];
} Record;
void addRecord(FILE *file, int id, const char *data) {
Record record;
record.id = id;
strcpy(record.data, data);
fwrite(&record, sizeof(Record), 1, file);
}
void readRecords(FILE *file) {
Record record;
rewind(file);
while (fread(&record, sizeof(Record), 1, file) == 1) {
printf("ID: %d, Data: %s\n", record.id, record.data);
}
}
int main() {
FILE *file = fopen("sequential.dat", "wb+");
if (!file) {
perror("fopen");
return 1;
}
addRecord(file, 1, "First record");
addRecord(file, 2, "Second record");
addRecord(file, 3, "Third record");
readRecords(file);
fclose(file);
return 0;
}