C++ – how to create a new file in c++ using windows.h


I want to know if there are any methods other than file.open(); in <windows.h>

Best Solution

As you specifically state windows.h, the WINAPI function CreateFile() can used to create a file. At the end of the link there are multiple examples of using CreateFile(), but here is a simple one:

#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
    if (h)
        std::cout << "CreateFile() succeeded\n";
        std::cerr << "CreateFile() failed:" << GetLastError() << "\n";
    return 0;