Skip to content

Instantly share code, notes, and snippets.

@tsterker
Created December 20, 2011 22:47
Show Gist options
  • Save tsterker/1503664 to your computer and use it in GitHub Desktop.
Save tsterker/1503664 to your computer and use it in GitHub Desktop.
[c] Get filesize
/* Returns file size in BYTES */
int file_sizeof(FILE *fp)
{
int filesize;
fseek(fp, 0L, SEEK_END); /* jump to end of file */
filesize = ftell(fp); /* current byte of file == filesize */
rewind(fp); /* jump to beginning of file */
return filesize;
}
int main(){
FILE *fp;
char *filename = "file.dat";
int filesize;
fp = fopen(filename,"wb") /* check if not NULL */
filesize = file_sizeof(fp);
printf("size of %s is %d\n", filename, filesize);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment