-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadFile.c
More file actions
48 lines (38 loc) · 739 Bytes
/
ReadFile.c
File metadata and controls
48 lines (38 loc) · 739 Bytes
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
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
size_t read_all_bytes(const char * fileName,void ** mem)
{
FILE * file;
struct stat mystat;
void * content;
*mem = NULL;
if(0 != stat(fileName,&mystat))
{
return 0;
}
file = fopen(fileName,"rb");
if(!file)
{
return 0;
}
content = malloc(mystat.st_size + 1);
if(content == NULL)
{
return 0;
}
((char*)content)[mystat.st_size] = 0;
if( mystat.st_size != fread(content,1,mystat.st_size,file))
{
goto FREE_CONTENT;
}
*mem = content;
return mystat.st_size;
FREE_CONTENT:
free(content);
return 0;
}
void free_all_bytes(void * mem)
{
free(mem);
}