Created
July 17, 2019 18:59
-
-
Save AregevDev/49e7bad6d60ac1290699d2f6d9e34fbf to your computer and use it in GitHub Desktop.
Function for reading files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char *readFile(const char *filepath) { | |
char *source = NULL; | |
FILE *fp = fopen(filepath, "r"); | |
if (fp) { | |
if (fseek(fp, 0L, SEEK_END) == 0) { | |
long bufsize = ftell(fp); | |
source = malloc(sizeof(char) * (bufsize + 1)); | |
fseek(fp, 0L, SEEK_SET); | |
size_t len = fread(source, sizeof(char), bufsize, fp); | |
source[len++] = '\0'; | |
} | |
fclose(fp); | |
} | |
return source; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment