-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_and_write.c
96 lines (73 loc) · 2.44 KB
/
read_and_write.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
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include "Headers/stb_image.h"
// Write a struct to file
int writeStructToFile(char *filename, Image *data) {
// Open the file in the 'wb' (write-binary) mode
FILE *fl = fopen(filename, "wb");
// Check if the file doesn't exist
if (fl == NULL) {
return 1; // Return 1 if the file doesn't exist
}
// Assign the dSize variable to the size of the data
size_t dSize = sizeof(data);
// Check if dSize is the same size as the written data
if (dSize != fwrite(data, sizeof(Image), dSize, fl)) {
return 1; // Return 1 because of an unsuccessful write
}
// Close the file
fclose(fl);
//free(data);
return 0; // Return 0 if the file was successfully written
}
// Checks if a struct of type Image is empty
int isEmpty(Image i) {
printf("Reached isEmpty\n");
if (i.width == 0 && i.height == 0 && i.channels == 0) {
return 1; // Return 1 because the Image is empty
} else {
return 0; // Return 0 because the Image is not empty
}
}
// Read an array of structs from a specified file
Image *readStructFromFile(char *filename) {
// Open the specified file in rb (read-binary)
FILE *fl = fopen(filename, "rb");
// Allocate enough memory to read the data from the file
Image *data;
// Read the file's data into the data pointer
fread(data, sizeof(Image), INT64_MAX, fl);
// Close the file as it is now no longer needed
fclose(fl);
// Return the data
return data;
}
void print(Image *image) {
for (int i; i < len(image); i++) {
Image current = image[i];
printf("\nTopic:\t%s\n", current.topic);
printf("Width:\t%d\n", current.width);
printf("Height:\t%d\n", current.height);
printf("Channels:\t%d\n", current.channels);
}
}
int main() {
// Create the image object
Image a;
// Assign the values to the image object
a.width = 6;
a.height = 9;
a.channels = 4;
a.topic = "wow";
// Write the Image object a to the file
writeStructToFile("Saves/saves.bin", &a);
// Read the file into the data pointer
Image *data = readStructFromFile("Saves/saves.bin");
// Define the current variable
Image current = data[0];
printf("Width: %d\n", current.width);
printf("Height: %d\n", current.height);
printf("Channels: %d\n", current.channels);
printf("Topic: %s\n", current.topic);
// Free the data variable
//free(data);
}