Last active
October 1, 2023 12:13
-
-
Save hapejot/b15d95a40220764c6436b5468ac7264b to your computer and use it in GitHub Desktop.
read_file
This file contains hidden or 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 *string_from_file( char *name ) { | |
int fd; | |
fd = open( name, O_RDONLY ); | |
assert( fd > 0 ); | |
struct stat buf; | |
fstat( fd, &buf ); | |
assert( S_ISREG( buf.st_mode ) ); | |
char *result = calloc( buf.st_size + 1, 1 ); | |
size_t rc = read( fd, result, buf.st_size ); | |
assert( rc == buf.st_size ); | |
close( fd ); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment