File Flags
When creating or opening files using Win32 API functions like CreateFile, you can specify various flags to control the behavior of the file handle and the underlying file system operations. These flags provide fine-grained control over aspects such as access permissions, sharing modes, and file attributes.
General Access Flags
These flags define the desired access to the file. You must specify at least one of these.
| Flag | Description |
|---|---|
GENERIC_READ |
Specifies read access to the object. |
GENERIC_WRITE |
Specifies write access to the object. |
GENERIC_EXECUTE |
Specifies execute access to the object. |
GENERIC_ALL |
Specifies all possible access rights for the object. |
Sharing Flags
These flags determine how the file can be shared with other processes. If this flag is omitted, the default behavior is to deny sharing.
| Flag | Description |
|---|---|
FILE_SHARE_READ |
Specifies that subsequent open requests that request read access to the object should succeed in this function. |
FILE_SHARE_WRITE |
Specifies that subsequent open requests that request write access to the object should succeed in this function. |
FILE_SHARE_DELETE |
Specifies that subsequent open requests that request delete access to the object should succeed in this function. |
0 |
The file is opened exclusively. Other processes cannot open the file. |
Creation Disposition Flags
These flags control how the file is created or opened. They determine what happens if the file already exists or does not exist.
| Flag | Description |
|---|---|
CREATE_NEW |
Creates a new file. If the file already exists, the function fails. |
CREATE_ALWAYS |
Creates a new file. If the file already exists, the function overwrites the file with the specified attributes and access permissions. |
OPEN_EXISTING |
Opens the file. If the file does not exist, the function fails. |
OPEN_ALWAYS |
Opens the file. If the file does not exist, the function creates a new file. |
TRUNCATE_EXISTING |
Opens the file and truncates it so that its size is zero bytes. If the file does not exist, the function fails. |
File Attribute Flags
These flags specify the attributes of the file. They are used when creating a new file or modifying attributes of an existing one.
| Flag | Description |
|---|---|
FILE_ATTRIBUTE_ARCHIVE |
The file has been archived. Applications use this to mark files for backup or removal. |
FILE_ATTRIBUTE_COMPRESSED |
The file is compressed. For a file, this means that all of the data in the file is compressed. |
FILE_ATTRIBUTE_DIRECTORY |
This attribute is a 32-bit value that indicates attributes of the file. This flag is for directories. |
FILE_ATTRIBUTE_HIDDEN |
The file is hidden. It is not included in an ordinary directory listing. |
FILE_ATTRIBUTE_NORMAL |
This attribute is a 32-bit value that indicates attributes of the file. This is the default value for a file. |
FILE_ATTRIBUTE_OFFLINE |
The file data is not immediately available. This means that the file data must be retrieved from a remote storage device. |
FILE_ATTRIBUTE_READONLY |
The file is read-only. Applications can read the file, but cannot write to it or delete it. |
FILE_ATTRIBUTE_SYSTEM |
The file is a system file. That is, the file is part of the operating system or is used exclusively by the operating system. |
FILE_ATTRIBUTE_TEMPORARY |
The file is temporary. File operations may flake if this attribute is not specified. |
CreateFile to specify file attributes, you typically combine desired attributes using the bitwise OR operator (|). For example, to create a read-only archive file, you might use FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_ARCHIVE.
Other Important Flags
| Flag | Description |
|---|---|
FILE_FLAG_DELETE_ON_CLOSE |
The file is to be deleted when the last handle to it is closed. |
FILE_FLAG_NO_BUFFERING |
The file is being opened with no system FILE_CACHE_FILE_INFORMATION. This flag does not affect the paging of files. |
FILE_FLAG_RANDOM_ACCESS |
The file is to be accessed randomly. The system can use this as a hint to optimize file access. |
FILE_FLAG_SEQUENTIAL_SCAN |
The file is to be accessed sequentially from beginning to end. The system can use this as a hint to optimize file access. |
FILE_FLAG_WRITE_THROUGH |
Writes to the file bypass the cache for that file. |
SECURITY_ANONYMOUS |
Specifies that the security context of the calling process should be used to impersonate the anonymous level. |
FILE_FLAG_NO_BUFFERING requires that applications correctly manage memory alignment for buffers and that all read/write operations are aligned to the sector size of the underlying storage device. Incorrect usage can lead to data corruption.
Example Usage (C++)
Here's a simplified example of how you might use these flags with CreateFile:
#include <windows.h>
#include <iostream>
int main() {
HANDLE hFile = CreateFile(
L"example.txt", // File name
GENERIC_WRITE | GENERIC_READ, // Access mode
FILE_SHARE_READ, // Sharing mode
NULL, // Security attributes
CREATE_ALWAYS, // Creation disposition
FILE_ATTRIBUTE_NORMAL, // File attributes
NULL // Template file
);
if (hFile == INVALID_HANDLE_VALUE) {
std::cerr << "Error creating file: " << GetLastError() << std::endl;
return 1;
}
std::cout << "File created/opened successfully." << std::endl;
// Perform file operations here...
CloseHandle(hFile); // Always close the handle when done
return 0;
}