Hi all,
What is the easiest way to convert a CString into a standard C++ sting.
Thanks
Jump to PostCString mystring = _TEXT("Hello World"); string astring = (LPCTSTR)mystring; // assuming UNICODE is NOT defined or string astring = mystring.GetBuffer(0);
Jump to Post>>How about this way...
You will have to try it to find out because I don't know if that will work or not with the most recent version of MFC supplied with VC++ 2005. Microsoft made a lot of changes to it, including making it a template instead of a …
Jump to PostThanks,
How about this way...
string MyStr( (const char*)MyCStringObject );
Old style cast.
Read:
http://msdn2.microsoft.com/en-us/library/ms174288(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/awkwbzyc(VS.80).aspx
Jump to Postdid you include <string> at the top of your program?
Jump to PostAre you certain you are talking about line 21? It would seem more reasonable that line 16 should give you that error because ss is a CString object and cout doesn't know out to output it, or CString doesn't have an << operator for cout unless of course you wrote …
CString mystring = _TEXT("Hello World");
string astring = (LPCTSTR)mystring; // assuming UNICODE is NOT defined
or
string astring = mystring.GetBuffer(0);
Thanks,
How about this way...
string MyStr( (const char*)MyCStringObject );
>>How about this way...
You will have to try it to find out because I don't know if that will work or not with the most recent version of MFC supplied with VC++ 2005. Microsoft made a lot of changes to it, including making it a template instead of a c++ class.
Thanks,
How about this way...
string MyStr( (const char*)MyCStringObject );
Old style cast.
Read:
http://msdn2.microsoft.com/en-us/library/ms174288(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/awkwbzyc(VS.80).aspx
Ok, I've done it in this way.
std::string strRTF(ss.GetString());
and no compiled error at all.
To check it simply I used
cout << strRTF;
, here I got a compile error.
binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
I can't find why is that. Everything is done on console application with MFC support. So this should work.
did you include <string> at the top of your program?
Of course. And also preceding everything with std:: but still it not work. Here is my code, why I'm confusing is the first cout works, but not the second one.
if(m_rtfCtrl.CreateEx(WS_EX_APPWINDOW, WS_BORDER|ES_MULTILINE, CRect(10,10,200,200), &x , 1))
{
CString ss;
CFile rtfFile;
BOOL err = rtfFile.Open("G:\\Work On\\CPP\\RTFControl\\TestFile.rtf", CFile::modeReadWrite, NULL);
int iLength = rtfFile.GetLength();// Data length
char *pBuffer = new char[iLength];// Data buffer
rtfFile.Read(pBuffer, iLength);
CString rtf(pBuffer);
m_rtfCtrl.SetWindowText(rtf);
m_rtfCtrl.GetWindowText(ss);
std::cout << ss << std::endl;
// CString into string
std::string strRTF(ss.GetString());
std::cout << strRTF << std::endl;
}
Are you certain you are talking about line 21? It would seem more reasonable that line 16 should give you that error because ss is a CString object and cout doesn't know out to output it, or CString doesn't have an << operator for cout unless of course you wrote it yourself.
I'm not clear. I got it in this way, line 16 can be effect on line 21? I've comments the line 16 and still the issue is there.
Hey, I found it. That strRTF should be a null-terminated string, so I change the line 21 as follows.
cout << strRTF.c_str();
But why it is necessary. Work on console application and cout is a standard iostream object.
Actually it doesn't really matter about that error because cout can not print to any MFC (or non-MFC) window. The output is just tossed into the bit bucket by the os. If you want to see debug messages then use MessageBox().
Or over load the << operator for an ostringstream inheriting class and pipe all the output to into a text box.
eg...
class mfc_text_box : public ostringstream
{
//pseudo -code
operator << (Cstring &x)
{
write_txt_to_box(x.c_str());
}
}
Then you can use a mfc_text_box_obj just like cout.
Actually it doesn't really matter about that error because cout can not print to any MFC (or non-MFC) window. The output is just tossed into the bit bucket by the os. If you want to see debug messages then use MessageBox().
Even I use a MessageBox() I have to use a null-terminated string there. :(
No you can use the CString object directly
CString str = _TEXT("Hello World");
AfxMessageBox(str,MB_OK);
or
CString str = _TEXT("Hello World");
MessageBox(str.GetBuffer(),"Debug Message",MB_OK);
Ya, that is true. But can't do this in my application with strRTF string without null-terminating.
AfxMessageBox(strRTF,MB_OK);
Not work,
AfxMessageBox(strRTF.c_str(),MB_OK);
Works.
Yes that is true. First parameter points to either CString object or null-terminated string. So here I have to use a null terminated string. Now its clear to me.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.