From: matthew Date: Sat, 18 Aug 2018 16:15:11 +0000 (+0100) Subject: implemented basic file read func X-Git-Url: https://git.owens.tech///git?a=commitdiff_plain;h=6fabd762332e7eb4aa2f638034bd16c332e34741;p=csrpg.git implemented basic file read func --- diff --git a/common/fileRead.c b/common/fileRead.c new file mode 100644 index 0000000..8bc7eb0 --- /dev/null +++ b/common/fileRead.c @@ -0,0 +1,31 @@ +#include "fileRead.h" +#include +#include + +char *readFile(const char *path) +{ + char *buffer = NULL; + int str_size, read_size; + FILE *handler = fopen(path, "r"); + + if(handler) + { + fseek(handler, 0, SEEK_END); + str_size = ftell(handler); + rewind(handler); + + // Allocating enough memory for the file and a null end char + buffer = malloc(sizeof(char) * (str_size + 1)); + read_size = fread(buffer, sizeof(char), str_size, handler); + + // capping off the string + buffer[str_size] = '\0'; + + if(str_size != read_size){ + free(buffer); + buffer = NULL; + } + } + + return buffer; +} diff --git a/common/fileRead.h b/common/fileRead.h new file mode 100644 index 0000000..7577b8f --- /dev/null +++ b/common/fileRead.h @@ -0,0 +1,5 @@ +#ifndef FILEREAD_H +#define FILEREAD_H + +char *readFile(const char *path); +#endif//FILEREAD_H