# char and strings

This topic is 3963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i have the following IDirect3DTexture9* tex = 0; char* texFN = d3dxmtrls.pTextureFilename; HR(D3DXCreateTextureFromFile(gd3dDevice, texFN, &tex)); but i need to append something that i have in a string to texFN, such as the following std::string directory = "C:\somwhere\" char* texFN = d3dxmtrls.pTextureFilename; texFN = directory + texFN; how would i do this? im sure ive used the following in the past, but it wont work now std::string directory = "C:\somwhere\" char* texFN = d3dxmtrls.pTextureFilename; char temp[]; sprintf(temp, %s%s, directory.data(), texFN;

##### Share on other sites
1. You should be using std::string all over the place except where you absolutely have to use const char*
2. The member function to get a const char* from a std::string is c_str(), not data(). data() isn't guaranteed to giev you a null terminated string of const char's at all (As far as I'm aware)
3. Are you sure that d3dxmtrls.pTextureFilename is large enough to have "C:\somwhere\" prepended to it?
4. Define "Won't work now"
5. Are you passing that exct string ("C:\somwhere\") in like that, or are you escaping it? That won't even compile currently.
6. Any reason you're not using forward slashes instead (Which work on Win32 exactly the same as backslashes except for a few specific cases) and don't require escaping?

Something like this should work:
char* texFN = d3dxmtrls.pTextureFilename;std::string strFilename = "C:/somewhere/";strFilename += texFN;HR(D3DXCreateTextureFromFile(gd3dDevice, strFilename.c_str(), &tex));
As would using a std::stringstream.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013247
×