fseek(f,0,2); How does this work without SEEK_CUR / SEEK_SET or SEEK_END?

Jeries Haddad picture Jeries Haddad · Jul 6, 2016 · Viewed 7.7k times · Source

I'm not being able to understand it, aren't we supposed to have SEEK_CUR / SEEK_SET or SEEK_END in the whence? Why does it have 2 and how does it work like this?

Answer

Ishay Peled picture Ishay Peled · Jul 6, 2016

SEEK_SET/SEEK_CUR/SEEK_END are 0/1/2 respectively, you can use the number or definition.

See definitions here: http://unix.superglobalmegacorp.com/BSD4.4/newsrc/sys/unistd.h.html

/* whence values for lseek(2) */
#define SEEK_SET    0   /* set file offset to offset */
#define SEEK_CUR    1   /* set file offset to current plus offset */
#define SEEK_END    2   /* set file offset to EOF plus offset */

Of course, it is bad practice to directly use these numbers as it may change in future implementations (not likely though)