tags:

views:

99

answers:

3

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

A: 

sprintf

/* sprintf example */
#include <stdio.h>

int main ()
{
  char buffer [50];
  for(int i=0; i < 3;i++){
     sprintf (buffer, "File%d.jpg", i);
     // buffer now contains the file1.jpg, then file2.jpg etc
  }
  return 0;
}
Preet Sangha
This doesn't produce wide strings, but the idea was ok and can be converted to `wsprintf`.
Ben Voigt
+5  A: 

I'd use _snwprintf:

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

You could also use a 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
R Samuel Klatchko
+1 : Better than mine.
Preet Sangha
That works, except I needed to prefix L to "MyImage...."
Mr Bell
@MrBell - thanks, I forgot about that important detail.
R Samuel Klatchko
+1  A: 

The most 'C++' way would be to use wstringstream:

#include <sstream>

//...

std::wstringstream ss;
ss << 3;
LPCWSTR str = ss.str().c_str();
Brian R. Bondy
For stringstream's you need to do `ss.str().c_str();`
R Samuel Klatchko
ya fixed at the same time
Brian R. Bondy