Results 1 to 9 of 9

Thread: CString into string

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink 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

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: CString into string

    Code:
    CString theCString = "Hello World!";
    std::string yourString(theCString.GetString());
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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,

    Code:
    cout << strRTF;
    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

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: CString into string

    Try preceding everything with std::.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: CString into string

    Try:
    Code:
    std::string strRTF((char*)ss.GetString());
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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

  8. #8
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

  9. #9

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    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
  •  



Click Here to Expand Forum to Full Width