File I/O
This section covers the core Windows API functions for performing input and output operations on files. These functions allow applications to create, read, write, and manage files on the file system.
Core Concepts
- Handles: File operations typically use handles, which are opaque identifiers representing an open file or device.
- File Access Rights: Specify the permissions required to access a file (e.g., read, write, execute).
- Sharing Modes: Define how other processes can access the file concurrently.
- File Pointers: Keep track of the current position within a file for reading or writing.
Key Functions
Creating and Opening Files
These functions are used to create new files or open existing ones.
| Function | Description |
|---|---|
CreateFile |
Creates or opens a file or I/O device. Returns a handle to the device. |
CreateFile2 |
A more robust and flexible version of CreateFile, introduced in Windows 8. |
Reading and Writing Data
Functions for transferring data between memory buffers and files.
| Function | Description |
|---|---|
ReadFile |
Reads data from a file or I/O device into a buffer. |
WriteFile |
Writes data from a buffer to a file or I/O device. |
ReadFileEx |
Reads data from a file or I/O device into a buffer, using asynchronous I/O. |
WriteFileEx |
Writes data from a buffer to a file or I/O device, using asynchronous I/O. |
Managing File Pointers
Functions to reposition the file pointer.
| Function | Description |
|---|---|
SetFilePointer |
Moves the file pointer to a specified location. |
SetFilePointerEx |
A more robust version of SetFilePointer that supports 64-bit offsets. |
SeekFilePointer |
(Deprecated, use SetFilePointerEx) Moves the file pointer. |
Closing Files
Releases the file handle and associated resources.
| Function | Description |
|---|---|
CloseHandle |
Closes an open object handle, including file handles. |
File Attributes and Information
Functions to retrieve or set file attributes and metadata.
| Function | Description |
|---|---|
GetFileAttributes |
Retrieves the attributes for a specified file or directory. |
SetFileAttributes |
Sets the attributes for a specified file or directory. |
GetFileSizeEx |
Retrieves the size of the specified file. |
Example: Basic File Writing
#include <windows.h>
#include <stdio.h>
int main() {
HANDLE hFile;
DWORD dwBytesWritten;
const char* message = "Hello, Windows API File I/O!";
const char* filename = "example.txt";
// Create or open the file
hFile = CreateFile(filename, // File name
GENERIC_WRITE, // Write access
0, // No sharing
NULL, // Default security attributes
CREATE_ALWAYS, // Overwrite if exists, create if not
FILE_ATTRIBUTE_NORMAL, // Normal file
NULL); // No template file
if (hFile == INVALID_HANDLE_VALUE) {
printf("Error opening file (%d).\n", GetLastError());
return 1;
}
// Write data to the file
if (!WriteFile(hFile, // Handle to file
message, // Data to write
strlen(message),// Number of bytes to write
&dwBytesWritten,// Number of bytes written
NULL)) { // Overlapped structure
printf("Error writing to file (%d).\n", GetLastError());
CloseHandle(hFile);
return 1;
}
printf("%d bytes written successfully.\n", dwBytesWritten);
// Close the file handle
CloseHandle(hFile);
printf("File closed.\n");
return 0;
}
Note: For robust error handling, always check the return value of file I/O functions and use
GetLastError() to retrieve specific error codes.
Warning: Using deprecated functions like
SeekFilePointer is discouraged. Prefer modern alternatives like SetFilePointerEx for better compatibility and features.
Advanced Topics
- Asynchronous I/O (Overlapped I/O) for non-blocking operations.
- File mapping for efficient memory access to file contents.
- Directory management functions (e.g.,
CreateDirectory,RemoveDirectory). - File locking for exclusive access.
- Unicode vs. ANSI character sets and their impact on file operations.
Explore the links in the sidebar for more detailed documentation on specific functions and related topics.