Reputation: 9338
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
Upvotes: 4
Views: 9395
Reputation: 596713
The Win32 API has multiple string formatting functions available, eg:
WCHAR buffer[SomeMaxLengthHere];
for(int i=0; i < 3;i++)
{
wsprintfW(buffer, L"%i", i);
...
}
WCHAR buffer[SomeMaxLengthHere];
for(int i=0; i < 3;i++)
{
StringCbPrintfW(buffer, sizeof(buffer), L"%i", I);
...
}
WCHAR buffer[SomeMaxLengthHere];
for(int i=0; i < 3;i++)
{
StringCchPrintfW(buffer, sizeof(buffer) / sizeof(WCHAR), L"%i", i);
...
}
Just to name a few.
Upvotes: 1
Reputation: 76541
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
Upvotes: 9
Reputation: 65516
wsprintf
/* wsprintf example */
#include <stdio.h>
int main ()
{
wchar_t buffer [50];
for(int i=0; i < 3;i++){
wsprintf (buffer, L"File%d.jpg", i);
// buffer now contains the file1.jpg, then file2.jpg etc
}
return 0;
}
Upvotes: 0
Reputation: 347336
The most 'C++' way would be to use wstringstream:
#include <sstream>
//...
std::wstringstream ss;
ss << 3;
LPCWSTR str = ss.str().c_str();
Upvotes: 3