-
Notifications
You must be signed in to change notification settings - Fork 212
File to C struct
Fedor Elizarov edited this page Feb 21, 2020
·
3 revisions
Corange provides a mechanism to load any file into a structure as long as it has a specific extension, lets load "*.num" into number:
typedef struct {
float value;
} number;First we create a loader function, any function with prototype number* number_load_file(char* filename) will do:
number* number_load_file(char* filename) {
FILE* file = fopen(filename, "r");
number* n = malloc(sizeof(number));
fscanf(file, "%f", &(n->value));
return n;
}Second, a destroyer:
void delete_number(number* n) {
free(n);
}Third, tell Corange about the functions:
asset_handler(number, "num", number_load_file, delete_number);Now you can use it:
number* num = asset_get(P("/number1.num"));