Saturday, May 25, 2013
 
    Search Web Link
Search Software
    
API Home » File » SetFilePointer

SetFilePointer

Category: File
Hits: 6524 Rating: SetFilePointer  has been rated 2.93 by  1299 users (2.93) votes 1299
Rate: 1-star  2-stars  3-stars  4-stars  5-stars
E-mail this API to friend E-mail this Code to a friend  save Bookmark this VB API Function
API Explanation
SetFilePointer moves the position of the file pointer of a currently open file. The file pointer identifies the position in a synchronous (non-overlapped) file where reading from and writing to the file begins. The file must have been opened with at least either read-level or write-level access (or both). The actual distance to move is a combination of lDistanceToMove and lpDistanceToMoveHigh. The binary or hexadecimal values of each are put one after another to create the actual value. Mathematically, it is determined by the formula lpDistanceToMoveHigh * 2^32 + lDistanceToMove. (If the value is negative, you have to perform a binary Not (inverse) on both values, and then add 1 to the lower half.) The function returns -1 if an error occured. If successful, the function returns the lower half of the new position of the file pointer, and the upper half is put into the variable passed as lpDistanceToMoveHigh, which can be combined in the same way. Remember that the first byte of the file is considered to have a position of 0.

Parameter Information
Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hFile _
As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh _
As Long, ByVal dwMoveMethod As Long) As Long

hFile
The handle of the open file to move the file pointer of.
lDistanceToMove
The lower half of the number of bytes to move the file pointer. For negative values, see the paragraph above.
lpDistanceToMoveHigh
Variable that contains the upper half of the number of bytes to move the file pointer. For negative values, see the paragraph
above. After the call, the variable receives the upper half of the new file pointer.
dwMoveMethod
Exactly one of the following flags specifying the reference point to move the file pointer from:
FILE_BEGIN = 0
The beginning of the file; i.e., the very first byte of the file.
FILE_CURRENT = 1
The current position of the file pointer.
FILE_END = 2
The end of the file; i.e., immediately after the very last byte of the file.
Code



' Read the third character from the beginning and the fifth character
' from the end of file C:\\Test\\myfile.txt. Note that some of the file access API functions need to use
' an alternate declare when used in Windows 95 or 98. See those functions' pages for more
' information.
Dim charbuffer As String * 1 ' receives single character from 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

' Use CreateFile's alternate declare because this isn't Win NT (see its page for the reason why).
hfile = CreateFileNS("C:\\Test\\myfile.txt", GENERAL_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE,
0)
If hfile = -1 Then ' the file could not be opened
Debug.Print "Unable to open the file -- probably does not exist."
End ' abort the program
End If

' 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 a single character and display it.
retval = ReadFileNO(hfile, ByVal charbuffer, 1, numread, 0) ' read the character
Debug.Print "Character at byte position 3: "; charbuffer

' 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 this character and display it.
retval = ReadFileNO(hfile, ByVal charbuffer, 1, numread, 0) ' read the character
Debug.Print "Character 5 bytes from the end position: "; charbuffer

retval = CloseHandle(hfile) ' close the file





HOME | MUSIC | DOWNLOADS |  GAMES | TEST-QUIZZES | SITEMAP | ABOUT | HELP
Copyright © 2000 - 2013 Ex-designz. All rights reserved. Website Developed By Dexter Zafra of Myasp-net.com
Link to us | Advertisement | Contact us | Privacy Policy | Terms of use | Accessibility