如何使用windows.h在c ++中创建一个新文件

gan*_*cpp 2 c++ windows file header-files

我想知道是否有比其他任何方法file.open();<windows.h>

hmj*_*mjd 5

正如您明确指出的那样windows.h,WINAPI函数CreateFile()可用于创建文件.在链接的末尾有多个使用示例CreateFile(),但这里有一个简单的例子:

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

int main()
{
    HANDLE h = CreateFile("test.txt",    // name of the file
                          GENERIC_WRITE, // open for writing
                          0,             // sharing mode, none in this case
                          0,             // use default security descriptor
                          CREATE_ALWAYS, // overwrite if exists
                          FILE_ATTRIBUTE_NORMAL,
                          0);
    if (h)
    {
        std::cout << "CreateFile() succeeded\n";
        CloseHandle(h);
    }
    else
    {
        std::cerr << "CreateFile() failed:" << GetLastError() << "\n";
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)