-
May 14th, 2010, 04:14 AM
#1
Problem with converting std::string to CString
Hi all,
I am experiencing a problem in converting std::string to CString, and I don't know why it occurs. I am not using MFC. I just included atlstr.h.
Here is the code I use:
string mpname = "SCCMDC.SCCM.COM";
CString sMPIP(mpname.c_str());
cout << "sMPIP: " << sMPIP << endl;
printf("Printf: %s", sMPIP);
printf("Printf2: %s", &sMPIP);
cout returns something like this:
sMPIP: 000000000003046F8
While the two printfs returns something like this:
Printf: <
Printf: H=1
I am not sure if the above is causing another error: adding two CStrings.
CString sNewloc = "something" + cString1 + " something " + cString2;
printf("new loc: %s", sNewloc);
Am I converting it wrong or am I just printing it wrong? Can anyone help me with this?
Thank you!
Last edited by LeanA; May 14th, 2010 at 04:36 AM.
Reason: Additional problem
-
May 14th, 2010, 05:21 AM
#2
Re: Problem with converting std::string to CString
I guess that cout is converting the CString to void *. Try casting
it to something that cout understand:
Code:
cout << "sMPIP: " << static_cast<LPCTSTR>(sMPIP) << endl;
-
May 14th, 2010, 08:37 AM
#3
Re: Problem with converting std::string to CString
I believe you meant LPCSTR, or ...
Code:
cout << "sMPIP: " << static_cast<const char*>(sMPIP) << endl;
Since cout is char based.
Do the same for printf("%s") parameters.
Here's how to use code-tags: http://www.codeguru.com/forum/misc.php?do=bbcode#code
gg
-
May 14th, 2010, 09:54 AM
#4
Re: Problem with converting std::string to CString
I'm surprised cout isn't working. But yes, printf() expects a char* for a %s. While CString contains an implicit conversion to char*, it will not work in a variadic argument list like printf has.
-
May 14th, 2010, 02:07 PM
#5
Re: Problem with converting std::string to CString
Originally Posted by Lindley
I'm surprised cout isn't working.
For wide strings, the stream to use is std::wcout. If you pass a wide string to cout, you get the addresses instead of the strings displayed.
Regards,
Paul McKenzie
-
May 15th, 2010, 08:27 AM
#6
Re: Problem with converting std::string to CString
Hi all, thank you all for replying.
I forgot to mention that I also tried wcout. It has the same output as in using cout.
Or does wcout not work with CStrings too?
Thank you!
-
May 15th, 2010, 08:05 PM
#7
Re: Problem with converting std::string to CString
Originally Posted by LeanA
Hi all, thank you all for replying.
I forgot to mention that I also tried wcout. It has the same output as in using cout.
Or does wcout not work with CStrings too?
Thank you!
There are two "versions" of CString, and it depends on the build that you are producing. If it's UNICODE build, then CString will contain a wide character string, else it will contain a string that is single-byte.
That is the first issue -- sort out what build you are producing, Unicode or not Unicode. If it's Unicode, then the stream must be std::wcout when printing wide character strings, else the stream to use is std::cout.
The second issue is that operator << knows nothing about CString -- that's why you get the hex output even if you are using std::wcout and you have a Unicode string.
To tell operator << how to handle CString, the CString class has a casting operator (LPCTSTR) that returns a const char* (or const w_chart*) to operator <<, which that operator is overloaded to handle.
So you have to make two things happen -- you use the correct stream (cout or wcout), depending on the build type (Unicode, non-Unicode) and you use the LPCTSTR cast operator for CString.
Regards,
Paul McKenzie
-
May 16th, 2010, 01:20 PM
#8
Re: Problem with converting std::string to CString
Code:
CStringA a = "Hello";
CStringW w = L"World";
wcout << static_cast<const wchar_t*>(a) << endl; // always an error
cout << static_cast<const char*>(w) << endl; // always an error
wcout << static_cast<LPCTSTR>(a) << endl; // compile error, or Ok
cout << static_cast<LPCTSTR>(w) << endl; // compile error, or operator<<(const void*) invoked
If you cast the CString based on the stream type, you'll always catch this mistake at compile time.
gg
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|