-3

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?

Ezrar
  • 23
  • 2
  • 2
    _"Are you able to assign the File name to a Variable?"_ Of course you are! Did you try it? Did it work? If not, what was ther error? – πάντα ῥεῖ Feb 25 '15 at 14:52
  • possible duplicate of [inserting int variable in file name](http://stackoverflow.com/questions/11437702/inserting-int-variable-in-file-name) – Raymond Chen Feb 25 '15 at 14:57
  • Sorry for the duplicate question, I admit this was a simple question, I was just confused with the different types of CreateFiles and the different types of parameters they accept. Next time I'll research more before posting since I'm still a noob at C++, but thank you for the quick replies and solutions (This is a great community). – Ezrar Feb 25 '15 at 15:47

1 Answers1

0

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.

David Heffernan
  • 587,191
  • 41
  • 1,025
  • 1,442
  • Thank you works perfectly. I didn't know you had to redeclare the variable data type when concating; thought it would work by string filename = "foo" + "bar"; – Ezrar Feb 25 '15 at 15:41