-
Notifications
You must be signed in to change notification settings - Fork 4
/
fileio.c
45 lines (37 loc) · 1.01 KB
/
fileio.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
#include <stdio.h>
#include <stdlib.h>
#include "fileio.h"
int read_file(char *filename, uint8_t **buf, size_t *size) {
FILE *fp;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Failed to load file (\"%s\").\n", filename);
return 1;
}
/* Get the file size */
fseek(fp, 0L, SEEK_END);
size_t allocsize = ftell(fp) * sizeof(uint8_t);
rewind(fp);
*buf = (uint8_t *)malloc(allocsize);
if (*buf == NULL) {
fprintf(stderr,
"Error allocating mem for file (file=%s, size=%zu byte).",
filename, allocsize);
fclose(fp);
return 1;
}
*size = fread(*buf, sizeof(uint8_t), allocsize, fp);
fclose(fp);
return 0;
}
int save_file(char *filename, uint8_t *buf, size_t size) {
FILE *fp;
fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "Failed to open file (\"%s\").\n", filename);
return 1;
}
fwrite(buf, sizeof(uint8_t), size, fp);
fclose(fp);
return 0;
}