Skip to content

Instantly share code, notes, and snippets.

@AregevDev
Created July 17, 2019 18:59
Show Gist options
  • Save AregevDev/49e7bad6d60ac1290699d2f6d9e34fbf to your computer and use it in GitHub Desktop.
Save AregevDev/49e7bad6d60ac1290699d2f6d9e34fbf to your computer and use it in GitHub Desktop.
Function for reading files
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