读书人

CString 和string的转换,该怎么解决

发布时间: 2012-01-18 00:23:26 作者: rapoo

CString 和string的转换
网上好多地方都说CString->string。只需要通过其构造函数即可。如
CString mfcstr("hello");
string str(mfcstr);
可是我验证了一下,根本不可以,
其他转换也是,好多方法都不行。
大家给提供个方法, 一定是自己验证过的。
我用的vs2010。

[解决办法]

C/C++ code
#include <afx.h>#include <iostream>#include <string>using namespace std;int _tmain(int argc, _TCHAR* argv[]){    CString str("寒江雪是一个好人!");    setlocale(LC_ALL,"chs");#ifdef UNICODE    wstring name=str.GetBuffer();    wcout<<name<<endl;#else    string word=str.GetBuffer();    cout<<word<<endl;#endif    string sen("Hero,May god be with you!");    CString csen=sen.c_str();#ifdef UNICODE    wcout<<csen.GetBuffer()<<endl;#else    cout<<csen.GetBuffer()<<endl;#endif    system("pause");    return 0;}
[解决办法]
探讨
网上好多地方都说CString->string。只需要通过其构造函数即可。如
CString mfcstr("hello");
string str(mfcstr);
可是我验证了一下,根本不可以,
其他转换也是,好多方法都不行。
大家给提供个方法, 一定是自己验证过的。
我用的vs2010。

读书人网 >VC/MFC

热点推荐