user2264410
user2264410

Reputation: 267

Trying to append a wstring

I am trying to append an integer to wstring:

TCHAR buffer[MAX_PATH]={0};
GetModuleFileName(NULL, buffer, sizeof(buffer)/sizeof(*buffer));
TCHAR* fileName = PathFindFileName(buffer);
std::wstring name(fileName);

std::wstring temp;   
temp = _wgetenv(L"TEMP");
temp.append(L"\\-deploy-temp-");
temp.append(rand()); <-- gives an error; can't convert it to wstring
temp.append(L"\\");
temp.append(name);

Thank you in advance.

Here what I've attempted:

std::wstring to_wstring(rand());

Apparently, this is supposed to work in C++11, but I have MSVC2010, so I don't think it compiles on my setup.

Upvotes: 3

Views: 9554

Answers (1)

Kiril Kirov
Kiril Kirov

Reputation: 38163

Try

#include <iostream>
#include <sstream>
#include <string>

// ...
std::wstring wstr;

std::wstringstream wss;
wss << rand();

wstr.append( wss.str() );

std::wcout << wstr;
//...

Upvotes: 6

Related Questions