Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
SetFilePointer changes the position of the file pointer in an open file. The pointer can be moved either forwards or backwards relative to the beginning of the file, the current file pointer position, or the end of the file. The file pointer cannot be moved more than one byte past the last byte of the file (where it would point to the end of the file).
When calculating the value for lDistanceToMove and the value to set the variable passed as lpDistanceToMoveHigh for negative numbers (i.e., moving the file pointer backwards), you cannot simply take the negative of the absolute value. First put the absolute values into those parameters. For lpDistanceToMoveHigh, perform a bitwise NOT operation on the value. For lDistanceToMove, perform a bitwise NOT operation followed by adding 1. See the example for a demonstration of this procedure, which is necessary to product a 64-bit negative number from a composite of the two dwords.
If an error occured, the function returns -1 (use GetLastError to get the error code). If successful, the function returns the low-order dword of the new file pointer position. The high-order dword is placed into the variable passed as lpDistanceToMoveHigh.
None.
Const FILE_BEGIN = 0
Const FILE_CURRENT = 1
Const FILE_END = 2
' Read two four-character strings from the file "C:\Test\myfile.txt":
' one starting at the third byte from the beginning, the other starting
' five bytes from the end.
Dim strbuf As String * 4 ' receives four-byte string from the file
Dim lowbyte As Long, highbyte As Long ' components of file pointer position
Dim numread As Long ' receives number of bytes read from file
Dim hFile As Long ' handle of the open file
Dim retval As Long ' return value
' Open the file for read-level access, if it already exists.
hFile = CreateFile("C:\Test\myfile.txt", GENERAL_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
If hFile = -1 Then ' the file could not be opened
Debug.Print "Unable to open the file -- it probably does not exist."
End ' abort the program
End If
' Keep in mind that, although the lpDistanceToMoveHigh parameter
' must be passed a variable, the lDistanceToMove parameter can be
' passed either a variable or a literal constant. For clarity, the
' following code uses a variable for lDistanceToMove.
' Set the file pointer to 2 bytes after the beginning of the file -- i.e., the third byte position.
lowbyte = 2: highbyte = 0 ' this equals 2
lowbyte = SetFilePointer(hFile, lowbyte, highbyte, FILE_BEGIN) ' move the file pointer there
' Now read four characters and display them.
retval = ReadFile(hFile, ByVal strbuf, 4, numread, ByVal CLng(0))
Debug.Print "Four-character string starting at byte 3: "; strbuf
' Set the file pointer to 5 bytes before the beginning of the file. Note how the lowbyte and highbyte
' numbers must be manipulated to represent a negative value.
lowbyte = (Not 5) + 1: highbyte = Not 0 ' this represents -5
lowbyte = SetFilePointer(hFile, lowbyte, highbyte, FILE_END) ' move the file pointer there
' Now read four characters from here and display them.
retval = ReadFile(hFile, ByVal strbuf, 4, numread, ByVal CLng(0))
Debug.Print "Four-character string starting at the fifth byte from the end: "; strbuf
' Close the file.
retval = CloseHandle(hFile)
Last Modified: October 14, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information