Skip to content

Instantly share code, notes, and snippets.

@hapejot
Last active October 1, 2023 12:13
Show Gist options
  • Save hapejot/b15d95a40220764c6436b5468ac7264b to your computer and use it in GitHub Desktop.
Save hapejot/b15d95a40220764c6436b5468ac7264b to your computer and use it in GitHub Desktop.
read_file
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