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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

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
ZoppoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.