File Management Functions
The Windows API provides a comprehensive set of functions for interacting with the file system. These functions allow for creating, opening, reading, writing, and deleting files, as well as managing directories and file attributes.
Creating and Opening Files
Use these functions to create new files or open existing ones for various operations.
Creates or opens a file or I/O device. This is one of the most versatile file I/O functions.
HANDLE CreateFile(
LPCSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
Parameters:
lpFileName: The name of the file to be created or opened.dwDesiredAccess: The generic access rights to the file (e.g.,GENERIC_READ,GENERIC_WRITE).dwShareMode: How the file will be shared.dwCreationDisposition: How to create or open the file.dwFlagsAndAttributes: File attributes and flags.hTemplateFile: A handle to a template file for the file being created.
Opens an existing file. It's an older function but still relevant for certain compatibility scenarios.
int OpenFile(
LPCSTR lpszName,
OFSTRUCT *lpReOpenBuff,
UINT fuStyle
);
Parameters:
lpszName: The name of the file to open.lpReOpenBuff: Pointer to anOFSTRUCTstructure that receives information about the file.fuStyle: Flags that control the opening of the file.
Reading from and Writing to Files
Once a file handle is obtained, these functions are used to transfer data.
Reads data from a file or I/O device. Reads up to a specified number of bytes from the file handle into the buffer.
BOOL ReadFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);
Parameters:
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 anOVERLAPPEDstructure for asynchronous I/O.
Writes data to a file or I/O device. Writes up to a specified number of bytes from the buffer to the file.
BOOL WriteFile(
HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped
);
Parameters:
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 anOVERLAPPEDstructure for asynchronous I/O.
Closing and Manipulating Files
Essential functions for managing file resources and obtaining file information.
Closes an open object handle. This is crucial for releasing resources associated with files, processes, and threads.
BOOL CloseHandle(
HANDLE hObject
);
Parameters:
hObject: A handle to an open object.
Sets the end-of-file position for the specified file. This can truncate or extend a file.
BOOL SetEndOfFile(
HANDLE hFile
);
Parameters:
hFile: Handle to the file.
Retrieves the size of the specified file.
BOOL GetFileSizeEx(
HANDLE hFile,
PLARGE_INTEGER lpFileSize
);
Parameters:
hFile: Handle to the file.lpFileSize: Pointer to aLARGE_INTEGERstructure that receives the file size.
Directory Management
Functions for creating, deleting, and navigating through directories.
Creates a new directory. If the parent directory does not exist, the function fails.
BOOL CreateDirectory(
LPCSTR lpPathName,
LPSECURITY_ATTRIBUTES lpAttribute
);
Parameters:
lpPathName: The path of the directory to create.lpAttribute: Security attributes.
Deletes an existing empty directory.
BOOL RemoveDirectory(
LPCSTR lpPathName
);
Parameters:
lpPathName: The path of the directory to remove.
GetLastError() to retrieve more specific error information.