Convert "CString" to "String"

I'm use Visual Studio 2oo5.  I'm use "mfc  Visual C++" . How can I convert  data type "CString" to datatype "string" ?
LVL 3
shaf81Asked:
Who is Participating?
 
ZoppoConnect With a Mentor Commented:
ok, I guess you use UNICODE or MBCS.

First error should be go away if you init cStr like this:

> CString cStr = _T( "Hello, world!" );

Second should go away if you use STL's wstring instead of string, i.e.:

> std::wstring str = (LPCTSTR)cStr;

Hope that helps,

ZOPPO
0
 
ZoppoCommented:
Hi shaf81;

if you mean STL's string class with 'string' you can do simply like this:

CString cStr = "Hello, world!";
std::string str = (LPCTSTR)cStr;

Hope that helps,

ZOPPO
0
 
shaf81Author Commented:
output:-  

>------ Build started: Project: dialler1, Configuration: Debug Win32 ------
1>Compiling...
1>dialler1Dlg.cpp
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(619) : error C2039: 'CString' : is not a member of 'std'
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(619) : error C2440: 'initializing' : cannot convert from 'const char [14]' to 'ATL::CStringT<BaseType,StringTraits>'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC<wchar_t>
1>        ]
1>        Constructor for class 'ATL::CStringT<BaseType,StringTraits>' is declared 'explicit'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC<wchar_t>
1>        ]
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(620) : error C2440: 'initializing' : cannot convert from 'LPCTSTR' to 'std::basic_string<_Elem,_Traits,_Ax>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>Build log was saved at "file://e:\sip\sip_2\dialler1\dialler1\Debug\BuildLog.htm"
1>dialler1 - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ZoppoCommented:
Hi,

IMO this can only happen if you anywhere wrote 'std::CString' - that's of course wrong. CString is not part of STL.

Maybe it would be helpful if you could post a relevant part of your code ...

ZOPPO
0
 
shaf81Author Commented:
output:-
Ignore above output that I earlier provided.

1>------ Build started: Project: dialler1, Configuration: Debug Win32 ------
1>Compiling...
1>dialler1Dlg.cpp
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(619) : error C2440: 'initializing' : cannot convert from 'const char [14]' to 'ATL::CStringT<BaseType,StringTraits>'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC<wchar_t>
1>        ]
1>        Constructor for class 'ATL::CStringT<BaseType,StringTraits>' is declared 'explicit'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC<wchar_t>
1>        ]
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(620) : error C2440: 'initializing' : cannot convert from 'LPCTSTR' to 'std::basic_string<_Elem,_Traits,_Ax>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(1389) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>Build log was saved at "file://e:\sip\sip_2\dialler1\dialler1\Debug\BuildLog.htm"
1>dialler1 - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

CString cStr = "Hello, world!";
	std::string str = (LPCTSTR)cStr;

Open in new window

0
 
shaf81Author Commented:
Hi ZOPPO,

You are genious. Can you explain how could you get to that answer?
You are accurately correct.

Thank you very much !!!
0
 
shaf81Author Commented:
If you explain about how would you got that  answer it will really helpful for me. Any way your answer is perfect and really helpful.
Thank you very much !!!
0
 
ZoppoCommented:
Hi again,

well, my first comment based on the fact I know that 'std::string' has a constructor which takes a pointer to a char array. The LPCTSTR-cast operator of CString returns such a pointer.

As you posted the errors I found the corrections like this:

1. I know that the 'cannot convert from 'const char [14]' to 'ATL::CStringT<BaseType,StringTraits>' can only happen if the char-type used with CString isn't 'char'. The char-type used for CString is TCHAR, which is a typedef for 'WCHAR' (which is a typedef for native 'wchar_t') in unicode, otherwise TCHAR is a typedef for 'char'. So, due to experience I at once found the problem must be you use unicode and in unicode you have to declare string literals with the '_T(...)'-macro (BTW, you can use this macro even if you don't use unicode)

2. Even from my experience I know using unicode with STL requires std::wstring instead of std::string. This is due to the fact how these two are typedef'ed:
> typedef basic_string<char, char_traits<char>, allocator<char> >      string;
> typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring;

That's all I can tell about how I found it - it's mostly just a matter of experience ;o)

Have a nice day,

best regards,

ZOPPO
0
 
shaf81Author Commented:
hi zoppo,

Thank you very much guy,  

Wish you all the best !!!

Have a nice day !!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.