File I/O
This section provides comprehensive documentation on the Windows API functions and concepts related to file input and output operations. Efficiently manage files, directories, and their associated metadata.
Core Concepts
Understanding the fundamental principles of file handling in Windows is crucial for robust application development. This includes concepts like file handles, asynchronous I/O, and buffering.
- File Handles: Unique identifiers returned by the system when a file is opened.
- File Pointers: Position within a file for read/write operations.
- Buffering: Techniques to improve I/O performance by using temporary memory areas.
- Asynchronous I/O: Allowing applications to continue processing while I/O operations are in progress.
Key Functions
CreateFileW
Creates or opens a file or I/O device. This is the primary function for obtaining a file handle.
lpFileName: The name of the file or device.dwDesiredAccess: The requested access to the file (e.g.,GENERIC_READ,GENERIC_WRITE).dwShareMode: The requested sharing mode (e.g.,FILE_SHARE_READ).lpSecurityAttributes: Security descriptor.dwCreationDisposition: Action to take if file exists or does not exist.dwFlagsAndAttributes: File attributes and flags.hTemplateFile: Handle to a template file.
- A handle to the opened file or device.
INVALID_HANDLE_VALUEon failure.
CreateFileW) for Unicode support.
ReadFile
Reads data from a file or I/O device into an application buffer.
hFile: Handle to the file.lpBuffer: Pointer to the buffer that receives the data.nNumberOfBytesToRead: Number of bytes to read.lpNumberOfBytesRead: Pointer to the number of bytes actually read.lpOverlapped: Pointer to an OVERLAPPED structure for asynchronous I/O.
TRUEif successful.FALSEon failure.
WriteFile
Writes data to a file or I/O device from an application buffer.
hFile: Handle to the file.lpBuffer: Pointer to the buffer containing the data to write.nNumberOfBytesToWrite: Number of bytes to write.lpNumberOfBytesWritten: Pointer to the number of bytes actually written.lpOverlapped: Pointer to an OVERLAPPED structure for asynchronous I/O.
TRUEif successful.FALSEon failure.
CloseHandle
Closes an open object handle. This includes file handles.
hObject: Handle to the object to be closed.
TRUEif the handle was closed successfully.FALSEif the handle is invalid.
Directory Operations
Manage directories and their contents using the following functions:
CreateDirectoryW: Creates a directory.RemoveDirectoryW: Removes an empty directory.FindFirstFileW/FindNextFileW: Enumerates files in a directory.GetFullPathNameW: Retrieves the full path for a specified file name.
Advanced Topics
Explore advanced file I/O techniques:
- Memory-Mapped Files: Efficiently access file contents as if they were in memory.
- File Stream I/O: Using C++ streams for file operations (e.g.,
fstream). - File System Notifications: Reacting to changes in the file system.
- Unicode and ANSI Character Sets: Handling text encoding correctly.
Related Topics
| Topic | Description |
|---|---|
| File Attributes | Understanding the different attributes assigned to files and directories. |
| File Locking | Mechanisms for controlling access to files to prevent data corruption. |
| Error Handling | Strategies for managing and reporting errors in file operations. |