C++如何勾住createfile的demo
时间: 2023-08-09 16:05:57 浏览: 110
以下是一个使用C++调用CreateFile函数的示例代码:
```C++
#include <windows.h>
#include <iostream>
int main() {
HANDLE hFile;
LPCSTR lpFileName = "test.txt";
DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
DWORD dwShareMode = 0;
LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
DWORD dwCreationDisposition = CREATE_ALWAYS;
DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
HANDLE hTemplateFile = NULL;
hFile = CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
if (hFile == INVALID_HANDLE_VALUE) {
std::cout << "Failed to create file" << std::endl;
return 1;
}
std::cout << "File created successfully" << std::endl;
CloseHandle(hFile);
return 0;
}
```
这个程序创建了一个名为“test.txt”的文件,并且在控制台输出“File created successfully”表示文件创建成功。你可以根据自己的需要修改文件名、访问权限和其他参数。
阅读全文