Prev Next

SetFilePointer info  Overview  Group

The SetFilePointer function moves the file pointer of an open file.

DWORD SetFilePointer(

    HANDLE hFile,

// handle of file

    LONG lDistanceToMove,

// number of bytes to move file pointer

    PLONG lpDistanceToMoveHigh,

// address of high-order word of distance to move

    DWORD dwMoveMethod 

// how to move

   );

Parameters

hFile
Identifies the file whose file pointer is to be moved. The file handle must have been created with GENERIC_READ or GENERIC_WRITE access to the file.
lDistanceToMove
Specifies the number of bytes to move the file pointer. A positive value moves the pointer forward in the file and a negative value moves it backward.
lpDistanceToMoveHigh
Points to the high-order word of the 64-bit distance to move. If the value of this parameter is NULL, SetFilePointer can operate only on files whose maximum size is 2^32 - 2. If this parameter is specified, the maximum file size is 2^64 - 2. This parameter also receives the high-order word of the new value of the file pointer.
dwMoveMethod
Specifies the starting point for the file pointer move. This parameter can be one of the following values:

Value

Meaning

FILE_BEGIN

The starting point is zero or the beginning of the file. If FILE_BEGIN is specified, DistanceToMove is interpreted as an unsigned location for the new file pointer.

FILE_CURRENT

The current value of the file pointer is the starting point.

FILE_END

The current end-of-file position is the starting point.

Return Values

If the SetFilePointer function succeeds, the return value is the low-order doubleword of the new file pointer, and if lpDistanceToMoveHigh is not NULL, the function puts the high-order doubleword of the new file pointer into the LONG pointed to by that parameter.

If the function fails and lpDistanceToMoveHigh is NULL, the return value is 0xFFFFFFFF. To get extended error information, call GetLastError.

If the function fails, and lpDistanceToMoveHigh is non-NULL, the return value is 0xFFFFFFFF and GetLastError will return a value other than NO_ERROR.

Remarks

You cannot use the SetFilePointer function with a handle to a nonseeking device, such as a pipe or a communications device. To determine the file type for hFile, use the GetFileType function.

You should be careful when setting the file pointer in a multithreaded application. For example, an application whose threads share a file handle, update the file pointer, and read from the file must protect this sequence by using a critical section object or mutex object. For more information about these objects, see Mutex Objects and Critical Section Objects.

If the hFile file handle was opened with the FILE_FLAG_NO_BUFFERING flag set, an application can move the file pointer only to sector-aligned positions. A sector-aligned position is a position that is a whole number multiple of the volume’s sector size. An application can obtain a volume’s sector size by calling the GetDiskFreeSpace function. If an application calls SetFilePointer with distance-to-move values that result in a position that is not sector-aligned and a handle that was opened with FILE_FLAG_NO_BUFFERING, the function fails, and GetLastError returns ERROR_INVALID_PARAMETER.

Note that if the return value is 0xFFFFFFFF and if lpDistanceToMoveHigh is non-NULL, an application must call GetLastError to determine whether the function has succeeded or failed. The following sample code illustrates this point:

// 

// Case One: calling the function with 

//           lpDistanceToMoveHigh == NULL 

 

// try to move hFile's file pointer some distance 

dwPointer = SetFilePointer (hFile, lDistance, 

                            NULL, FILE_BEGIN) ; 

 

// if we failed ... 

if (dwPointer == 0xFFFFFFFF) { 

 

    // obtain the error code 

    dwError = GetLastError() ; 

 

    // deal with that failure 

    . 

    . 

    . 

 

    } // end of error handler 

 

 

// 

// Case Two: calling the function with 

//           lpDistanceToMoveHigh != NULL 

 

// try to move hFile's file pointer some huge distance 

dwPointerLow = SetFilePointer (hFile, lDistanceLow, 

                               & lDistanceHigh, FILE_BEGIN) ; 

 

// if we failed ... 

if (dwPointerLow == 0xFFFFFFFF 

    && 

    (dwError = GetLastError()) != NO_ERROR ){ 

 

    // deal with that failure 

    . 

    . 

    . 

 

    } // end of error handler 

 

See Also

GetDiskFreeSpace, GetFileType, ReadFile, ReadFileEx, WriteFile, WriteFileEx