Skip to content

Instantly share code, notes, and snippets.

@yui0
Created October 23, 2017 02:31
Show Gist options
  • Save yui0/9613941498c58e5cdf5a22359d8ca061 to your computer and use it in GitHub Desktop.
Save yui0/9613941498c58e5cdf5a22359d8ca061 to your computer and use it in GitHub Desktop.
Get the file size
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