-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handler.h
More file actions
37 lines (33 loc) · 910 Bytes
/
file_handler.h
File metadata and controls
37 lines (33 loc) · 910 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
#ifndef FILE_HANDLER_H_ /* Include guard */
#define FILE_HANDLER_H_
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *read_file(char *filename) {
FILE *f = fopen(filename, "rt");
assert(f);
fseek(f, 0, SEEK_END);
long length = ftell(f); //-8 to remove extra chars that are somehow added to end of the file
fseek(f, 0, SEEK_SET);
char *buffer = (char *) calloc(1,length + 1);
buffer[length] = '\0';
fread(buffer, 1, length, f);
fclose(f);
return buffer;
}
bool file_exist(char *filename){
FILE *f;
if((f = fopen(filename,"r"))!=NULL)
{
// file exists
fclose(f);
return true;
}
else
{
//File not found, no memory leak since 'file' == NULL
//fclose(file) would cause an error
return false;
}
}
#endif