Skip to content

Instantly share code, notes, and snippets.

@quantumsheep
Created December 6, 2018 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save quantumsheep/ef32c46383c4083b6c93687ac6feffda to your computer and use it in GitHub Desktop.
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)
#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;
}
@Cewein
Copy link

Cewein commented Dec 9, 2018

you can replace fseek(f, origin, SEEK_SET) by rewind(f) do the same as getting origin and setting it back to the origin but it's in one function and in stdio.h

so no more need to use origin variable and it make the code shorter

@quantumsheep
Copy link
Author

you can replace fseek(f, origin, SEEK_SET) by rewind(f) do the same as getting origin and setting it back to the origin but it's in one function and in stdio.h

so 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