implemented basic file read func
authormatthew <matthew@owens.tech>
Sat, 18 Aug 2018 16:15:11 +0000 (17:15 +0100)
committermatthew <matthew@owens.tech>
Sat, 18 Aug 2018 16:15:11 +0000 (17:15 +0100)
common/fileRead.c [new file with mode: 0644]
common/fileRead.h [new file with mode: 0644]

diff --git a/common/fileRead.c b/common/fileRead.c
new file mode 100644 (file)
index 0000000..8bc7eb0
--- /dev/null
@@ -0,0 +1,31 @@
+#include "fileRead.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+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 (file)
index 0000000..7577b8f
--- /dev/null
@@ -0,0 +1,5 @@
+#ifndef FILEREAD_H
+#define FILEREAD_H
+
+char *readFile(const char *path);
+#endif//FILEREAD_H