|
-
Dec 13th, 2007, 08:26 PM
#1
Thread Starter
PowerPoster
CString into string
Hi all,
What is the easiest way to convert a CString into a standard C++ sting.
Thanks
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 13th, 2007, 09:17 PM
#2
Re: CString into string
Code:
CString theCString = "Hello World!";
std::string yourString(theCString.GetString());
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Dec 13th, 2007, 09:22 PM
#3
Thread Starter
PowerPoster
Re: CString into string
Thanks, I've done it in this way also.
Code:
string strRTF = (LPCSTR)ss;
Not given any compilation error. But when I try this,
It gives the following compile error. No idea why it is impossible.
binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 13th, 2007, 09:36 PM
#4
Re: CString into string
Try preceding everything with std::.
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Dec 13th, 2007, 09:41 PM
#5
Thread Starter
PowerPoster
Re: CString into string
I've already test it. Use the namespaces as well. But no luck. Amazing thing is,
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;
}
In my above code, fist cout works fine. The second cout not works.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 13th, 2007, 09:49 PM
#6
Re: CString into string
Try:
Code:
std::string strRTF((char*)ss.GetString());
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Dec 13th, 2007, 10:31 PM
#7
Thread Starter
PowerPoster
Re: CString into string
It also don't work chem. I found that strRTF should be null-terminated string. So,
Code:
cout << strRTF.c_str();
gives the correct string. But why is that, cout is the standard iostream object, so null-terminated is required?
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
-
Dec 13th, 2007, 10:48 PM
#8
Re: CString into string
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..
Code:
CString str = "Hello World!\0";
std::string str2((char*)str.GetString());
std::cout << str2;
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Dec 13th, 2007, 10:59 PM
#9
Thread Starter
PowerPoster
Re: CString into string
Yes, on this code also I have null-terminated the string str2.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
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
|