Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

odd wostringstream problem

Hello,

I thought this would trace the value "10" twice -

wostringstream OldSetting;
wostringstream OldSetting2;

WORD wValue = 10;
int iValue=10;

OldSetting << wValue;
TRACE(OldSetting.str().c_str());

OldSetting2 << iValue;
TRACE(OldSetting2.str().c_str());

but the WORD (or unsigned int) value does not appear. With different values I sometimes get garbage coming out. In this case I got nothing at all.

If I use ostringstream then it works as expected. Is this a bug or have I done something wrong?

This is with Visual Studio 2003.

Thanks.

Peter
[670 byte] By [PeterS2] at [2007-11-11 8:45:54]
# 1 Re: odd wostringstream problem
Obviously, you can't expect a wchar_t stream to be implicitly converted to char * by calling c_str while still getting meaningful results... If you need to use c_str for some reason then you should stick to char based streams. Also, check teh definitio of TRACE. What kind of strings does it expect?
Danny at 2007-11-11 21:01:05 >
# 2 Re: odd wostringstream problem
Thanks - I thought TRACE catered for that but it doesn't. I changed my test to the following code and it works fine now.

wostringstream OldSetting;
wostringstream OldSetting2;

unsigned int wValue = 10;
int iValue=10;

OldSetting << _T("word value is: ") << wValue << _T("\n");
wcout << OldSetting.str().c_str();

OldSetting2 << _T("int value is: ") << iValue << _T("\n");
wcout << OldSetting2.str().c_str();
PeterS2 at 2007-11-11 21:02:05 >