Reputation: 4185
What happened if I use fseek
fseek(fileptr,10000L,SEEK_CUR);
and it returns a nonzero value, as an indicator that it could not successfully execute, because the file was not big enough for the step.
At what position is the position indicator now? At the end of the file, or did it stay on the position it used to be, before execution?
The context:
I want to figure out a way of extracting the size of a file, which has more bytes than long could save safely. Therefore need to go step by step through the file and express the last few bytes with a fraction of a normal step.
E.g: 2,5 * 100 = size of 250Bytes with a step size of 100Bytes
Upvotes: 1
Views: 282
Reputation: 22261
After a failed seek, the file position should remain the same as whatever it was before.
I was going to suggest that you use lseek
instead of your hack to move ahead in the file by multiple steps because lseek
works with the off_t
type which should hopefully be a 64-bit type for you (see also #define _FILE_OFFSET_BITS
) but a quick check of some manpages reveals that there is also fseeko()
which would be handy for you since you're using FILE *
files, not raw file descriptors. Either way, the best way to seek right to the end of a file is to use one of those functions with SEEK_END
.
EDIT: aix make makes an important point which bears repeating: seeking past the end of the file does not make the seek fail.
Upvotes: 1
Reputation: 500277
fseek()
won't fail because the file is "not big enough for the step". Provided fileptr
is a valid stream, the position indicator will move to the previous position plus 10000L
bytes, even if this is past the end of the file.
It is possible for fseek()
to fail (for example, if the second argument wasn't one of the SEEK_xxx
constants). In this case, the position indicator will remain unchanged.
Upvotes: 2