ReadFile Function
Reads data from a specified file or input/output (I/O) device.
For Universal Windows Platform (UWP) apps: Use the Windows.Storage.Streams API.
Syntax
BOOL ReadFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);
Parameters
| Parameter | Type | Description |
|---|---|---|
hFile |
HANDLE |
A handle to the file or I/O device (such as a serial port or communications device) that contains the data to be read. The handle must have been created with the |
lpBuffer |
LPVOID |
A pointer to the buffer that receives the data read from the file or device. |
nNumberOfBytesToRead |
DWORD |
The maximum number of bytes to be read. This value must not be greater than the size of the buffer pointed to by |
lpNumberOfBytesRead |
LPDWORD |
A pointer to a 32-bit variable that receives the number of bytes read by this function.
If |
lpOverlapped |
LPOVERLAPPED |
A pointer to an If
If |
Return Value
If the function succeeds, and the lpOverlapped parameter is not NULL and the hEvent member of the OVERLAPPED structure is not NULL, the function returns TRUE. In this case, the hEvent is signaled when the read operation has been completed.
If the function succeeds, and lpOverlapped is NULL or the hEvent member is NULL, the function returns TRUE and the number of bytes read is returned in the variable pointed to by lpNumberOfBytesRead.
If the function fails, it returns FALSE. To get extended error information, call GetLastError.
Important: If lpOverlapped is not NULL and hEvent is NULL, the function will not return until the read operation has been completed. This is a synchronous operation.
Remarks
If the file has been opened with the FILE_FLAG_OVERLAPPED flag, the ReadFile function can be performed either synchronously or asynchronously.
- If
lpOverlappedisNULLor if thehEventmember of theOVERLAPPEDstructure isNULL, theReadFilefunction performs a synchronous read. The function does not return until the data has been read or an error occurs. - If
lpOverlappedis notNULLand thehEventmember is notNULL, the read operation is performed asynchronously. The function returnsFALSEandGetLastErrorreturnsERROR_IO_PENDINGif the read operation cannot be completed immediately. In this case, thehEventmember is signaled when the read operation has been completed.
If the file handle is opened with FILE_SHARE_READ or FILE_SHARE_WRITE, other processes can open the file to read or write, potentially affecting the data read by ReadFile.
The file pointer is advanced by the number of bytes read. If the file pointer is beyond the end of the file, ReadFile reads zero bytes and returns TRUE, with *lpNumberOfBytesRead set to 0.
To read data from a file opened for asynchronous I/O, use a separate thread to perform the read operation or use the completion routine notification capability of the OVERLAPPED_COMPLETION_ROUTINE.
If lpNumberOfBytesRead is NULL, the function can fail.
If lpOverlapped is NULL, the lpNumberOfBytesRead parameter must be valid.
If hFile is a handle to a console buffer, the ReadFile function reads input from the console buffer.
If hFile is a handle to a communications resource, the ReadFile function behaves according to the settings of the communications resource specified by the CreateFile function.
If the file is opened with CreateFile and the FILE_ATTRIBUTE_NORMAL attribute, ReadFile reads from the file normally.
If the file is opened with CreateFile and the FILE_FLAG_NO_BUFFERING attribute, you must read data in multiples of the sector size of the underlying device. You also must align the buffer and the file offset.
Use the GetDeviceCaps function to determine the sector size of the device.
Examples
For a code example, see Reading and Writing Files.
Requirements
| Attribute | Value |
|---|---|
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header | fileapi.h (include windows.h) |
| Library | Use Kernel32.lib |
| DLL | Kernel32.dll |
See Also
CreateFile
CloseHandle
GetFileSizeEx
SetFilePointer
WriteFile
OVERLAPPED
Device Input and Output Control
File Management Functions