File I/O Operations

Applies to: Windows Operating System

This section provides detailed information on the various Application Programming Interfaces (APIs) available in the Windows operating system for performing file input and output operations. These functions allow applications to create, read, write, and manage files and directories.

CreateFile

Creates or opens a file or I/O device.

The CreateFile function returns a handle to an open file or device. This handle can be used by other I/O functions to perform operations on the file or device.

Parameter Type Description
lpFileName LPCTSTR The name of the file or device to be created or opened.
dwDesiredAccess DWORD The requested access to the file or device. This can be one or more generic access rights (e.g., GENERIC_READ, GENERIC_WRITE).
dwShareMode DWORD A bitmask that specifies the way an application requests sharing of a file or device with other threads.
lpSecurityAttributes LPSECURITY_ATTRIBUTES A pointer to a SECURITY_ATTRIBUTES structure that contains the security descriptor for the file or device.
dwCreationDisposition DWORD Specifies an action to take if a file that is specified by lpFileName exists and what to do if it does not exist.
dwFlagsAndAttributes DWORD The file attributes and optional flags for the file or device.
hTemplateFile HANDLE A handle to a template file with the CREATE_ALWAYS flag set to create a new file.

Example:

HANDLE hFile = CreateFile(
    TEXT("MyFile.txt"),          // File name
    GENERIC_WRITE,               // Desired access
    0,                           // Share mode
    NULL,                        // Security attributes
    CREATE_ALWAYS,               // Creation disposition
    FILE_ATTRIBUTE_NORMAL,       // Flags and attributes
    NULL                         // Template file
);

if (hFile == INVALID_HANDLE_VALUE) {
    // Handle error
    DWORD error = GetLastError();
    // ...
} else {
    // File opened or created successfully
    // ...
    CloseHandle(hFile);
}
Note: Ensure proper error handling using GetLastError() after calling CreateFile.

ReadFile

Reads data from a file or input buffer.

The ReadFile function reads data from the specified file or input buffer into the buffer supplied by the user. Starting from the position indicated by the file pointer of the opened file, this function reads up to the number of bytes specified by nNumberOfBytesToRead.

Parameter Type Description
hFile HANDLE A handle to the file or I/O device (returned by CreateFile).
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 read.
lpOverlapped LPOVERLAPPED A pointer to an OVERLAPPED structure, which is required for asynchronous I/O operations.

Example:

DWORD bytesRead;
char buffer[1024];

if (!ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead, NULL)) {
    // Handle error
} else {
    buffer[bytesRead] = '\0'; // Null-terminate the string
    // Process the data in 'buffer'
}

WriteFile

Writes data to a file or output buffer.

The WriteFile function writes data to the specified file or input buffer. Starting from the current file pointer, this function writes up to the number of bytes specified by nNumberOfBytesToWrite.

Parameter Type Description
hFile HANDLE A handle to the file or I/O device.
lpBuffer LPCVOID A pointer to a buffer that contains the data to be written to the file.
nNumberOfBytesToWrite DWORD The number of bytes to write to the file.
lpNumberOfBytesWritten LPDWORD A pointer to a variable that receives the number of bytes written.
lpOverlapped LPOVERLAPPED A pointer to an OVERLAPPED structure.

Example:

const char* data = "Hello, Windows File I/O!";
DWORD bytesWritten;

if (!WriteFile(hFile, data, strlen(data), &bytesWritten, NULL)) {
    // Handle error
} else {
    // Data written successfully
}

CloseHandle

Closes an open object handle.

The CloseHandle function closes a single handle to an open object. This function ensures that system resources associated with the handle are released.

Parameter Type Description
hObject HANDLE A handle to an open object.

Example:

if (hFile != INVALID_HANDLE_VALUE) {
    CloseHandle(hFile);
    hFile = INVALID_HANDLE_VALUE; // Good practice to reset the handle
}
Tip: Always close file handles when they are no longer needed to prevent resource leaks.

File Management Functions