c++winapibmpcreatefilelpcstr

Assigning Variable to CreateFileA file name


I'm creating a .bmp file using this CreateFileA method

HANDLE hFile = CreateFileA("Screenshot01.bmp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); At the moment is static and just keeps re-writing the old file.

I want to call this method multiple time, each time it will create a new file with a different file name, for example

"Screenshot01.bmp" "Screenshot02.bmp" "Screenshot03.bmp" etc.

It doesn't have to increment, but the file name should be different each time.

How do I change the file name each time the method is called? Are you able to assign the File name to a Variable?


Solution

  • Use a std::string. For instance:

    #include <string>
    ....
    std::string filename = "Screenshot01.bmp";
    HANDLE hFile = CreateFileA(filename.c_str(), ...);
    

    To build the file name up from an integer you might do this:

    #include <string>
    ....
    std::string filename = "Screenshot" + std::to_string(id) + ".bmp";
    HANDLE hFile = CreateFileA(filename.c_str(), ...);
    

    How do I change the file name each time the method is called?

    Keep track of the most recently used id value, and increment it when you need a new value.