Get location of a file pointer

In Windows, is there no way to get the current location of a file pointer?

For example, if I use SetFilePointer, and I want to determine whereabouts in the file the file pointer wound up after the seek?

Upvotes: 4

Views: 10588

Answers (3)

tletnes
tletnes

Reputation: 1998

You can use:

high_dword = 0;

low_dword = SetFilePointer(file, 0, &high_dword, FILE_CURRENT);

pos = high_dword << 32 | low_dword

Upvotes: 9

arx
arx

Reputation: 16904

SetFilePointer returns the new file pointer, but only works for file offsets < 4GB. If you want to handle larger files use SetFilePointerEx.

If you just want to retrieve the current position use FILE_CURRENT with an offset of zero.

Upvotes: 2

Rob Kennedy
Rob Kennedy

Reputation: 163357

The return value of that function tells you the new position. Quoting the documentation:

If the function succeeds and lpDistanceToMoveHigh is NULL, the return value is the low-order DWORD of the new file pointer.

Note If the function returns a value other than INVALID_SET_FILE_POINTER, the call to SetFilePointer has succeeded. You do not need to call GetLastError.

If function succeeds and lpDistanceToMoveHigh is not NULL, the return value is the low-order DWORD of the new file pointer and lpDistanceToMoveHigh contains the high order DWORD of the new file pointer.

If you want to discover the current location without moving it, then pass a "move method" of FILE_CURRENT and a distance of zero.

The documentation also contains an example of how to wrap that function into one that doesn't require splitting the upper and lower portions of the position into separate variables.

Upvotes: 8

Related Questions