Documentation for Windows Operating System Components
This section covers the Win32 API functions for performing file input and output operations on the Windows operating system. These functions allow applications to create, read, write, and manage files and directories.
Creates or opens a file or I/O device (such as a disk drive, partition, or communications resource).
HANDLE CreateFile(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
Name | Type | Description |
---|---|---|
lpFileName |
LPCTSTR |
The name of the file or device. |
dwDesiredAccess |
DWORD |
The generic access rights to the file. |
dwShareMode |
DWORD |
The mode in which the file may be shared. |
lpSecurityAttributes |
LPSECURITY_ATTRIBUTES |
A pointer to a SECURITY_ATTRIBUTES structure. |
dwCreationDisposition |
DWORD |
Determines the action to take if the file exists or does not exist. |
dwFlagsAndAttributes |
DWORD |
File attributes and flags. |
hTemplateFile |
HANDLE |
A handle to a template file with the desired attributes. |
If the function succeeds, the return value is an open handle to the specified file or device. If the function fails, the return value is INVALID_HANDLE_VALUE
.
CloseHandle
to close the handle when you are finished with it.lpSecurityAttributes
parameter.Reads data from a file or I/O device. Works with files opened with both synchronous and overlapped access.
BOOL ReadFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);
Name | Type | Description |
---|---|---|
hFile |
HANDLE |
A handle to the file or device for which read information is requested. |
lpBuffer |
LPVOID |
A pointer to the buffer that receives the data read from the file. |
nNumberOfBytesToRead |
DWORD |
The maximum number of bytes to be read. |
lpNumberOfBytesRead |
LPDWORD |
A pointer to a variable that receives the number of bytes actually read. |
lpOverlapped |
LPOVERLAPPED |
A pointer to an OVERLAPPED structure. |
If the function succeeds, and the dwNumberOfBytesToRead parameter is non-zero, the return value is TRUE. If the function succeeds, and dwNumberOfBytesToRead is zero, the return value is TRUE and lpNumberOfBytesRead is zero. If the function fails, the return value is FALSE.
lpOverlapped
is NULL, the function returns immediately after reading the specified number of bytes into the buffer.lpOverlapped
is not NULL, the function may return before the read operation is complete (asynchronous operation).Writes data to a file or I/O device. Works with files opened with both synchronous and overlapped access.
BOOL WriteFile(
HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped
);
Name | Type | Description |
---|---|---|
hFile |
HANDLE |
A handle to the file or device. The handle must have been created with the GENERIC_WRITE access right. |
lpBuffer |
LPCVOID |
A pointer to the buffer containing the data to be written. |
nNumberOfBytesToWrite |
DWORD |
The number of bytes to write to the file. |
lpNumberOfBytesWritten |
LPDWORD |
A pointer to a variable that receives the number of bytes actually written. |
lpOverlapped |
LPOVERLAPPED |
A pointer to an OVERLAPPED structure. |
If the function succeeds, and the nNumberOfBytesToWrite parameter is non-zero, the return value is TRUE. If the function succeeds, and nNumberOfBytesToWrite is zero, the return value is TRUE and lpNumberOfBytesWritten is zero. If the function fails, the return value is FALSE.
lpOverlapped
is NULL, the function returns immediately after writing the specified number of bytes from the buffer.lpOverlapped
is not NULL, the function may return before the write operation is complete (asynchronous operation).Moves the file pointer of the specified file to a location within the file.
DWORD SetFilePointer(
HANDLE hFile,
LONG lDistanceToMove,
PLONG lpDistanceToMoveHigh,
DWORD dwMoveMethod
);
Name | Type | Description |
---|---|---|
hFile |
HANDLE |
A handle to the file. |
lDistanceToMove |
LONG |
The number of bytes to move the file pointer. |
lpDistanceToMoveHigh |
PLONG |
A pointer to the high-order 32 bits of the number of bytes to move the file pointer. |
dwMoveMethod |
DWORD |
The starting point for the move. |
If the function succeeds, the return value is the new value of the file pointer. If the function fails, the return value is INVALID_SET_FILE_POINTER
.
SetFilePointerEx
for 64-bit file pointer support.dwMoveMethod
parameter can be one of the following: FILE_BEGIN
, FILE_CURRENT
, or FILE_END
.Closes an open object handle.
BOOL CloseHandle(
HANDLE hObject
);
Name | Type | Description |
---|---|---|
hObject |
HANDLE |
A handle to an open object. |
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.