-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_reader.c
More file actions
32 lines (27 loc) · 746 Bytes
/
file_reader.c
File metadata and controls
32 lines (27 loc) · 746 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
#include "common.h"
char *file_reader(char *file_path) {
FILE *p_file = fopen(file_path, "r");
char c;
char *file_content = NULL;
if (p_file == NULL) {
printf("File not found\n");
printf("\nExited with code 1\n");
exit(1);
}
int i = 0;
file_content = (char *) malloc(0 * sizeof(char));
while ((c = fgetc(p_file)) != EOF)
{
file_content = realloc(file_content, (i + 1) * sizeof(char));
if (file_content == NULL) {
printf("Memory allocation failed\n");
file_content = NULL;
return file_content;
}
file_content[i] = c;
i++;
}
file_content[i] = '\0';
fclose(p_file);
return file_content;
}