Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int read_from_file(char *fname, char **content)
- {
- int fh;
- int outbytes, len;
- struct stat statbuf;
- char *buf;
- if ( (fh=open(fname, O_RDONLY, 0)) == -1 )
- {
- /* Complain, explain */
- char msg[128];
- sprintf( msg, "failure opening file [%.64s]\n", fname );
- errorMessage( msg );
- return -1;
- }
- /* get file size */
- fstat(fh, &statbuf);
- outbytes = statbuf.st_size;
- /* read the file */
- buf = *content = (char *) malloc(outbytes+1);
- if ( (len = read(fh, buf, outbytes)) == -1 ) {
- char msg[128];
- sprintf( msg, "failure writing to file [%.64s]\n", fname );
- errorMessage( msg );
- return -1;
- }
- buf[outbytes] = '\0';
- return len;
- }
Add Comment
Please, Sign In to add comment