Storage Fundamentals
This section provides an overview and detailed reference for Windows APIs related to file system operations, data storage, and management. Understanding these APIs is crucial for developing applications that interact with the file system, manage data persistence, and ensure data integrity.
The Windows operating system provides a rich set of APIs that allow applications to access and manipulate files, directories, streams, and other storage-related objects. These APIs cover a wide range of functionalities, from basic file creation and deletion to advanced features like file locking, asynchronous I/O, and managing disk quotas.
Key Concepts in Windows Storage
- File System Objects: Understanding the difference between files, directories, symbolic links, and other file system entities.
- File I/O Operations: Reading, writing, seeking, and truncating files.
- Asynchronous I/O: Performing I/O operations without blocking the application thread, improving responsiveness.
- File Metadata: Accessing and modifying file attributes such as timestamps, permissions, and ownership.
- Volume Management: Working with disk volumes, partitions, and file system formats.
- Stream Operations: Utilizing alternate data streams for storing metadata or additional file content.
Core Storage APIs
The following table lists some of the most commonly used Win32 APIs for storage operations:
| API Function | Description | Related Topics |
|---|---|---|
CreateFile |
Creates or opens a file, directory, disk, or pipe. | File Handles, File Access Rights |
ReadFile |
Reads data from a file or communication device. | Asynchronous I/O |
WriteFile |
Writes data to a file or communication device. | Asynchronous I/O |
SetFilePointer |
Moves the file pointer for the specified file. | File Positioning |
CloseHandle |
Closes an open object handle. | File Handles |
GetFileAttributes |
Retrieves the attributes of a specified file or directory. | File Attributes |
CreateDirectory |
Creates a new directory. | Directory Management |
DeleteDirectory |
Deletes an existing directory. | Directory Management |
Example: Reading from a File
This example demonstrates how to open a file, read its contents into a buffer, and then display the contents.
#include <windows.h>
#include <stdio.h>
int main() {
HANDLE hFile;
char buffer[256];
DWORD bytesRead;
BOOL bRead;
// Open the file for reading
hFile = CreateFile(
L"example.txt", // File name
GENERIC_READ, // Desired access
FILE_SHARE_READ, // Share mode
NULL, // Security attributes
OPEN_EXISTING, // Creation disposition
FILE_ATTRIBUTE_NORMAL, // Flags & attributes
NULL); // Template file
if (hFile == INVALID_HANDLE_VALUE) {
printf("Error opening file: %lu\n", GetLastError());
return 1;
}
// Read from the file
bRead = ReadFile(
hFile, // Handle to the file
buffer, // Destination buffer
255, // Number of bytes to read
&bytesRead, // Number of bytes read
NULL); // Overlapped structure
if (bRead) {
buffer[bytesRead] = '\0'; // Null-terminate the string
printf("File content:\n%s\n", buffer);
} else {
printf("Error reading file: %lu\n", GetLastError());
}
// Close the file handle
CloseHandle(hFile);
return 0;
}