Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer to formatted LPCWSTR. C++

Tags:

c++

I have a direct3d project that uses D3DXCreateTextureFromFile() to load some images. This function takes a LPCWSTR for the path to file. I want to load a series of textures that are numbered consecutively (ie. MyImage0001.jpg, MyImage0002.jpg, etc) But c++'s crazy strings confuse me.

How do i:

for(int i=0; i < 3;i++)
{
//How do I convert i into a string path i can use with D3DXCreateTextureFromFile?
}

Edit:

I should mention I am using Visual Studio 2008's compiler

like image 865
Mr Bell Avatar asked Dec 12 '22 22:12

Mr Bell


1 Answers

One option is std::swprintf:

wchar_t buffer[256];
std::swprintf(buffer, sizeof(buffer) / sizeof(*buffer),
              L"MyImage%04d.jpg", i);

You could also use a std::wstringstream:

std::wstringstream ws;
ws << L"MyImage" << std::setw(4) << std::setfill(L'0') << i << L".jpg";
ws.str().c_str();  // get the underlying text array
like image 179
R Samuel Klatchko Avatar answered Dec 26 '22 15:12

R Samuel Klatchko