-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
37 lines (31 loc) · 813 Bytes
/
file.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
#include "file.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void file_initialize(File * file) {
file->size = 0;
file->data = NULL;
}
void file_read(File * file, const char * filename) {
FILE * fp = fopen(filename, "rb");
assert(fp != NULL);
fseek(fp, 0, SEEK_END);
file->size = ftell(fp);
file->data = (char *) realloc(file->data, file->size);
fseek(fp, 0, SEEK_SET);
size_t read_bytes = fread(file->data, 1, file->size, fp);
fclose(fp);
if (read_bytes != file->size) {
if (feof(fp))
printf("Error reading test.bin: unexpected end of file\n");
else if (ferror(fp)) {
perror("Error reading test.bin");
}
}
assert(read_bytes == file->size);
}
void file_deinitialize(File * file) {
file->size = 0;
free(file->data);
file->data = NULL;
}