Created
December 6, 2018 09:17
-
-
Save quantumsheep/ef32c46383c4083b6c93687ac6feffda to your computer and use it in GitHub Desktop.
Calculate a file length. The file need to be in `rb` mode (reading and binary mode)
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
#include <stdio.h> | |
int flen(FILE *f) | |
{ | |
int len; | |
int origin = ftell(f); | |
fseek(f, 0, SEEK_END); | |
len = ftell(f); | |
fseek(f, origin, SEEK_SET); | |
return len; | |
} |
you can replace
fseek(f, origin, SEEK_SET)
byrewind(f)
do the same as getting origin and setting it back to the origin but it's in one function and in stdio.hso no more need to use origin variable and it make the code shorter
You can't do this, rewind doesn't save the origin, it literally does fseek(f, 0, SEEK_SET)
, which doesn't really "rewind" the cursor
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can replace
fseek(f, origin, SEEK_SET)
byrewind(f)
do the same as getting origin and setting it back to the origin but it's in one function and in stdio.hso no more need to use origin variable and it make the code shorter