-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_and_read_binary_structs.c
85 lines (71 loc) · 1.86 KB
/
write_and_read_binary_structs.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct
{
int age;
char * name;
double iq;
} Person;
typedef struct
{
int age;
int mileage;
char * name;
char * brand;
}Tractor;
bool write_data(char *filename, Person *studentData, Tractor *tractorData);
void print_data(char *filename);
int main()
{
Person joshua;
Tractor tim;
joshua.name = "Joshua";
joshua.age = 28;
joshua.iq = 129;
tim.age = 55;
tim.mileage = 3500030;
tim.name = "Tim";
tim.brand = "Ford";
bool writeSuccess = write_data("struct_file.bin",&joshua,&tim);
print_data("struct_file.bin");
}
bool write_data(char *filename, Person *personData, Tractor *tractorData){
FILE *file;
file = fopen(filename, "wb");
if(file == NULL) return false;
if(fwrite(personData,sizeof(Person),1,file)!=1){
return false;
}
if(fwrite(personData,sizeof(Person),1,file)!=1){
return false;
}
if(fwrite(tractorData,sizeof(Tractor),1,file)!=1){
return false;
}
if (fclose(file) == EOF) return false;
return true;
}
void print_data(char *filename){
FILE *file;
// open the file with name filename in 'read a binary file mode'
file = fopen(filename, "rb");
// if fopen() failed to open the file, return NULL
if (file == NULL) return;
Person joshua;
Tractor tim;
fread(&joshua, sizeof(Person), 1, file);
printf("Name: %s\n\r",joshua.name);
printf("IQ: %lf\n\r",joshua.iq);
printf("Age: %d\n\r", joshua.age);
fread(&joshua, sizeof(Person), 1, file);
printf("Name: %s\n\r",joshua.name);
printf("IQ: %lf\n\r",joshua.iq);
printf("Age: %d\n\r", joshua.age);
fread(&tim, sizeof(tim), 1, file);
printf("Name: %s\n\r",tim.name);
printf("Mileage: %d\n\r",tim.mileage);
printf("Age: %d\n\r", tim.age);
printf("Brand: %s\n\r", tim.brand);
}