Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* filetobuf(char *file) //will reac a file into an allocated char pointer buffer
- {
- FILE *fptr;
- long length;
- char *buf;
- //opening the file
- fptr = fopen(file, "rb");
- struct stat statbuf;
- if(!fptr)
- {
- fprintf(stderr, "failed to open %s\n", file);
- return NULL;
- }
- //fseek(fptr,0,SEEK_END); //go to the end of the file
- //length = ftell(fptr); //count bytes in "fptr" file
- if(fstat(file,&statbuf) < 0)
- return 1;
- length = statbuf.st_size;
- buf = malloc(length+1); //allocate a buffer for all the file and +1 for the null terminator
- //fseek(fptr, 0, SEEK_SET); //SEEK_SET = Begging of file >> go to hte start of the file
- fread(fptr, 1, length, buf);
- fclose(fptr); //close the file
- free(fptr);
- buf[length] = 0; //null terminator
- return buf;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement