Created
March 30, 2013 23:53
-
-
Save zrbecker/5278880 to your computer and use it in GitHub Desktop.
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 *file2string(const char *path) | |
{ | |
FILE *fd; | |
long len, | |
r; | |
char *str; | |
if (!(fd = fopen(path, "r"))) | |
{ | |
fprintf(stderr, "Can't open file '%s' for reading\n", path); | |
return NULL; | |
} | |
fseek(fd, 0, SEEK_END); | |
len = ftell(fd); | |
printf("File '%s' is %ld long\n", path, len); | |
fseek(fd, 0, SEEK_SET); | |
if (!(str = malloc(len * sizeof(char)))) | |
{ | |
fprintf(stderr, "Can't malloc space for '%s'\n", path); | |
return NULL; | |
} | |
r = fread(str, sizeof(char), len, fd); | |
str[r - 1] = '\0'; /* Shader sources have to term with null */ | |
fclose(fd); | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment