Windows API Reference

File Management APIs

Explore the core APIs for interacting with the file system in Windows. These functions allow you to create, read, write, delete, and manage files and directories.

  • Comprehensive file and directory operations
  • Advanced security and access control
  • Efficient I/O operations
  • Support for various file system features

Core File Operations

These APIs form the foundation for managing files and directories within the Windows operating system.

Directory Management

Manage directories and their contents effectively with these APIs.

Advanced File Operations

Delve into more specialized functionalities for robust file handling.

Example: Reading File Content

Here's a simple C++ code snippet demonstrating how to read content from a file using the Windows API:

#include <windows.h>
#include <iostream>
#include <vector>

int main() {
    HANDLE hFile = CreateFile(L"example.txt",              // File name
                              GENERIC_READ,                 // Access mode
                              FILE_SHARE_READ,              // Share mode
                              NULL,                         // Security attributes
                              OPEN_EXISTING,                // Creation disposition
                              FILE_ATTRIBUTE_NORMAL,        // Flags and attributes
                              NULL);                        // Template file

    if (hFile == INVALID_HANDLE_VALUE) {
        std::cerr << "Error opening file: " << GetLastError() << std::endl;
        return 1;
    }

    DWORD fileSize = GetFileSize(hFile, NULL);
    if (fileSize == INVALID_FILE_SIZE) {
        std::cerr << "Error getting file size: " << GetLastError() << std::endl;
        CloseHandle(hFile);
        return 1;
    }

    if (fileSize == 0) {
        std::cout << "File is empty." << std::endl;
        CloseHandle(hFile);
        return 0;
    }

    std::vector<char> buffer(fileSize);
    DWORD bytesRead;

    if (!ReadFile(hFile, buffer.data(), fileSize, &bytesRead, NULL)) {
        std::cerr << "Error reading file: " << GetLastError() << std::endl;
        CloseHandle(hFile);
        return 1;
    }

    std::cout.write(buffer.data(), bytesRead);

    CloseHandle(hFile);
    return 0;
}
                    

For more detailed information, parameters, and return values, please refer to the specific API documentation linked above.