Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes read.

Syntax

BOOL ReadFile(
  HANDLE 
hFile,
  LPVOID 
lpBuffer,
  DWORD 
nNumberOfBytesToRead,
  LPDWORD 
lpNumberOfBytesRead,
  LPOVERLAPPED 
lpOverlapped
);

Parameters

hFile

[in] Handle to the file to be read. The file handle must have been created with GENERIC_READ access to the file. This parameter cannot be a socket handle.

lpBuffer

[out] Pointer to the buffer that receives the data read from the file.

nNumberOfBytesToRead

[in] Number of bytes to be read from the file.

lpNumberOfBytesRead

[out] Pointer to the number of bytes read. This function sets this value to zero before taking action or checking errors.

lpOverlapped

[in] Unsupported. Set to NULL.

Return Value

Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError.

If the return value is nonzero and the number of bytes read is zero, the file pointer was beyond the current end of the file at the time of the read operation.

Remarks

If part of the file is locked by another process and the read operation overlaps the locked portion, this function fails.

Accessing the input buffer while a read operation is using the buffer can cause corruption of the data read into that buffer. Applications must not read from, write to, reallocate, or free the input buffer that a read operation is using until the read operation completes.

When reading from a communications device, the behavior of this function is governed by the current communication time-outs as set and retrieved using the SetCommTimeoutsand the GetCommTimeoutsfunctions. Unpredictable results can occur if you fail to set the time-out values. For more information on communication time-outs, see COMMTIMEOUTS.

This function can fail and return ERROR_INVALID_USER_BUFFER or ERROR_NOT_ENOUGH_MEMORY when there are too many outstanding asynchronous I/O requests.

When a read operation reaches the end of a file, this function returns TRUE and sets * lpNumberOfBytesReadto zero. Windows Embedded CE does not support asynchronous read operations on files. The following code sample shows a test for the end of the file:

Copy Code
// Attempt a synchronous read operation.
bResult = ReadFile(hFile, &inBuffer, nBytesToRead,
&nBytesRead, NULL) ;
// Check for end of file.
if (bResult &&  (nBytesRead == 0) )
{
   // you are at the end of the file.
}

Requirements

Header winbase.h
Library coredll.lib
Windows Embedded CE Windows CE 1.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also