PDA

View Full Version : Problem with swprintf function



moowy
8th May 2007, 01:58
Hi,

I'm having troubles with using swprintf function. I'm trying to concatenate std::string with wide string and an integer like this:

wchar_t buffer[128];
swprintf( buffer, 128, L" Hello %s %d", /* std::string */ name , /* int */ number);

The problem is in std::string, because when I try to write the buffer to the screen it always tells me that the name is [null] -> but it isn't. Can anyone tell me what I'm doing wrong ?

wysota
8th May 2007, 12:03
Try name.c_str() instead of name.

moowy
8th May 2007, 13:51
I already tried it, but it doesn't do the trick for me. Instead of concatenating actual string like "michael" it concatenates characters like [] <- boxes....

Any ideas ?

wysota
8th May 2007, 14:09
What does the std::string contain (I mean, what encoding does it use)?

moowy
8th May 2007, 15:42
That's a good question. I read the string from the .xml file (which uses utf-8 encoding) so I guess the std::string should use the same (utf-8) encoding ?? Correct me if I'm wrong please.

wysota
8th May 2007, 19:25
I don't know if you're wrong but C strings (char* and possibly std::string) can handle strings that contain 0x00 characters and Unicode (and possibly UTF-8) encoding uses them. So I suggest not using std::string at all (in favour of unicode capable counterparts like wstring or QString).

moowy
8th May 2007, 20:47
I switched to std::wstring-s and I have no problems with them and swprintf function. It works like charm :) . Tnx for your help