Saturday, November 21, 2009
 
    Search Web Link
Search Software
    
API Home » File » ReadFile

ReadFile

Category: File
Hits: 5868 Rating: ReadFile has been rated 3.14 by  402 users (3.14) votes 402
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
ReadFile reads data from an open file and puts the data in the variable passed as lpBuffer. The function also puts the number of bytes of data actually read into the variable passed as lpNumberOfBytesRead. The file must of course have been opened with read-level access. The function starts reading from the position specified by the file pointer and sets the file pointer to the position immediately after the data read if the file is synchronous (non-overlapped). If it is asynchronous (overlapped), the reading starts at the point specified by lpOverlapped. If the end of the file is reached, the function completes successfully but gives a value of 0 for the number of bytes read. For Visual Basic users, the alternate declare must be used when not using an overlapped file; pass a value of 0 as lpOverlapped in the alternate declare. Note that Win 95/98 does not support overlapped files at all. The function returns 1 if successful, or 0 if an error occured.

Parameter Information
Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As _
Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, lpOverlapped As OVERLAPPED) As Long

Alternate Declare for use with synchronous (non-overlapped) files:
Declare Function ReadFileNO Lib "kernel32.dll" Alias "ReadFile" _
(ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long


hFile
The handle to the file to read from. The file must have read-level access.
lpBuffer
Variable that receives the data from the file. If this is a string, Visual Basic users must pass the string explicitly ByVal (see
example).
nNumberOfBytesToRead
The number of bytes of data to read from the file and put into lpBuffer (i.e., the size of lpBuffer).
lpNumberOfBytesRead
Receives the number of bytes of data actually read from the file. If this is 0, the end of the file has been reached.
lpOverlapped
Specifies where to begin reading from if the file is asynchronous (overlapped). If not, this must be 0. VB users need to use
the alternate Declares to pass 0 as this value.
Code



' Read both a Long (32-bit) number and a String from the file
' C:\\Test\\myfile.txt. Since this is under Win 95/98, the alternate declare is used. Notice how the
' ByVal keyword must be used when reading a string variable.
Dim longbuffer As Long ' receives long read from file
Dim stringbuffer As String ' receives string read from file
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

' Read a Long-type number from the file
retval = ReadFileNO(hfile, longbuffer, Len(longbuffer), numread, 0)
If numread = 0 Then ' EOF reached
Debug.Print "End of file encountered -- could not read any data."
Else
Debug.Print "Number read from file:"; longbuffer
End If

' Read a 10-character string from the file
stringbuffer = Space(11) ' make more than enough room in the buffer
retval = ReadFileNO(hfile, ByVal stringbuffer, 10, numread, 0)
If numread = 0 Then ' EOF reached
Debug.Print "End of file encountered -- could not read any data."
Else
Debug.Print "String read from file: "; String buffer
End If

retval = CloseHandle(hfile) ' close the file




HOME | ARTICLES | MUSIC | DOWNLOADS | LINK DIRECTORY |  GAMES | TEST-QUIZZES | SITEMAP | ABOUT | HELP
Copyright © 2000 - 2007 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