Reputation: 89
Aren't we supposed to use SEEK_CUR
/ SEEK_SET
or SEEK_END
for whence
? How does it work with just a fixed value?
Upvotes: 1
Views: 11519
Reputation: 20891
In stdio.h
,
#ifndef SEEK_SET
#define SEEK_SET 0 /* set file offset to offset */
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1 /* set file offset to current plus offset */
#endif
#ifndef SEEK_END
#define SEEK_END 2 /* set file offset to EOF plus offset */
#endif
Upvotes: 2
Reputation: 59
fseek() takes three parameters.
First one is the FILE * it will seek
Second is the int Record size, the value, it is expected to hop in one jump. Notice, it can be a negative value as well.
Third parameter is the offset value from where this hopping is to be done. There are three possible values for it 0,1 and 2. 0 represents beginning of the file 1 represents current location And, 2 represents the end of file.
The SEEK_END, SEEK_CUR or,the SEEK_SET are merely the Macro Expansions with values of 2,1 and 0 respectively. So when a user uses these macro Expansions rather that using the real values underneath, the pre-processors replaces every such usage with its respective value and compilation goes on as usual.
Upvotes: -1
Reputation: 8209
Look into your stdio.h
:
#define SEEK_END 2 /* Seek from end of file. */
So after preprocessing your fseek(f, 0, SEEK_END)
becames fseek(f, 0, 2)
and compiler will see 2
instead of meaningful name. As result we can avoid this name at all and put numerical value right now.
Upvotes: 2
Reputation: 391
Because SEEK_XXX are macros and have a certain value, in this case, SEEK_END is equal to 2, so it's the same fseek(f,0,SEEK_END) that fseek(f,0,2), but you should use always the macros. You can see this values for example in the notes of http://man7.org/linux/man-pages/man2/lseek.2.html (at the end).
Upvotes: 2
Reputation: 2868
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)
Upvotes: 9