Created
October 23, 2017 02:31
-
-
Save yui0/9613941498c58e5cdf5a22359d8ca061 to your computer and use it in GitHub Desktop.
Get the file size
This file contains 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
long fsize(char *name) | |
{ | |
struct stat stbuf; | |
int fd = open(name, O_RDONLY); | |
if (fd == -1) return -1; | |
if (fstat(fd, &stbuf) == -1) return -1; | |
close(fd); | |
return stbuf.st_size; | |
} | |
char *freadx(char *name) | |
{ | |
long size = fsize(name); | |
FILE *fp = fopen(name, "r"); | |
if (!fp) return 0; | |
char *p = (char*)calloc(size, sizeof(char)); | |
if (!fgets(p, size, fp)) return 0; | |
fclose(fp); | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment