A. Cakolli
A. Cakolli

Reputation: 33

How to make concatenations at const wchar_t* parameters?

How to make concatenations at const wchar_t* parameter in this case?

I'm trying to make screenshots that are saved automatically with names like:

screen-1.jpg
screen-2.jpg
screen-3.jpg
...
screen-i.jpg`

Code:

p_bmp->Save(L"C:/Users/PCUSER/AppData/screen-" + filenumber + ".jpg", &pngClsid, NULL);
 //filenumber is ant int that increases automatically

But it's giving me an error:

expression must have integral or unscoped

Upvotes: 3

Views: 229

Answers (1)

Mr.C64
Mr.C64

Reputation: 42964

Raw C-style string pointers (like const wchar_t*) cannot be concatenated together with a string semantic using operator+. However, you can concatenate instances of C++ string classes, like ATL CString or std::wstring, just to name a few.

Since you have also integer values to concatenate, you can first convert these to string objects (e.g using std::to_wstring()), and then use the overloaded operator+ to concatenate the various strings.

#include <string> // for std::wstring and to_wstring()
...

// Build the file name string using the std::wstring class
std::wstring filename = L"C:/Users/PCUSER/AppData/screen-";
filename += std::to_wstring(filenumber); // from integer to wstring
filename += L".jpg";

p_bmp->Save(filename.c_str(), // convert from wstring to const wchar_t*
            &pngClsid, 
            NULL);

Another approach you may follow if you use the ATL CString class is formatting the result string in a way similar to printf(), invoking the CString::Format() method, e.g.:

CStringW filename;
filename.Format(L"C:/Users/PCUSER/AppData/screen-%d.jpg", filenumber);

p_bmp->Save(filename, // implicit conversion from CStringW to const wchar_t*
            &pngClsid, 
            NULL);

Upvotes: 3

Related Questions