Hi all,
What is the easiest way to convert a CString into a standard C++ sting.
Thanks
Printable View
Hi all,
What is the easiest way to convert a CString into a standard C++ sting.
Thanks
chemCode:CString theCString = "Hello World!";
std::string yourString(theCString.GetString());
Thanks, I've done it in this way also.
Not given any compilation error. But when I try this,Code:string strRTF = (LPCSTR)ss;
It gives the following compile error. No idea why it is impossible.Code:cout << strRTF;
Quote:
binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
Try preceding everything with std::.
chem
I've already test it. Use the namespaces as well. But no luck. Amazing thing is,
In my above code, fist cout works fine. The second cout not works.Code: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;
}
Try:
chemCode:std::string strRTF((char*)ss.GetString());
It also don't work chem. I found that strRTF should be null-terminated string. So,
gives the correct string. But why is that, cout is the standard iostream object, so null-terminated is required?Code:cout << strRTF.c_str();
Strings that you store in a standard string object.. are still strings. They need to be null terminated.. how else would you know where they end?
Perhaps this works..
chemCode:CString str = "Hello World!\0";
std::string str2((char*)str.GetString());
std::cout << str2;
Yes, on this code also I have null-terminated the string str2.