Tim*_*ler 3 c++ linux createfile
我写了一个尝试创建文件的方法.但是我设置了标志CREATE_NEW,因此它只能在它不存在时创建它.它看起来像这样:
for (;;)
{
handle_ = CreateFileA(filePath.c_str(), 0, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_HIDDEN | FILE_FLAG_DELETE_ON_CLOSE, NULL);
if (handle_ != INVALID_HANDLE_VALUE)
break;
boost::this_thread::sleep(boost::posix_time::millisec(10));
}
Run Code Online (Sandbox Code Playgroud)
这应该是正常的.现在我想把它移植到linux,当然CreateFile函数只适用于windows.所以我在寻找与此相当的东西,但在Linux上.我已经看过open()但是我似乎找不到像CREATE_NEW一样的标志.有谁知道这方面的解决方案?
请查看open() 联机帮助页,组合O_CREAT和O_EXCL您正在寻找的内容.
例:
mode_t perms = S_IRWXU; // Pick appropriate permissions for the new file.
int fd = open("file", O_CREAT|O_EXCL, perms);
if (fd >= 0) {
// File successfully created.
} else {
// Error occurred. Examine errno to find the reason.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4191 次 |
| 最近记录: |