Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- char *fileContents(const char path[]) {
- struct stat st;
- stat(path, &st);
- FILE *file = fopen(path, "r");
- if (file == NULL) {
- return NULL;
- }
- char *s = (char*)calloc(st.st_size + 1, sizeof(char));
- if (s == NULL) {
- fclose(file);
- return NULL;
- }
- fread(s, sizeof(char), st.st_size, file);
- fclose(file);
- return s;
- }
- int main() {
- const char *s = fileContents("data.txt");
- puts(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement