您的位置:首页 > 其它

CString与string,char*的相互转换

2014-06-24 00:31 337 查看
在Win32 Console中使用MFC

#include <afx.h>
project--setting--general,MFC设置改为use MFC in a shared dll

CString转string和char*

#include <iostream>
#include <string>
#include "afx.h"
using namespace std;

int main()
{
CString cStr("CString");
//convert a CString to string
string s = CW2A(cStr);

//convert a CString to char *
CW2A newStr(cStr);
char *charArray = newStr;

cout<<cStr<<endl;
cout<<s<<endl;
cout<<charArray<<endl;

getchar();

}

string和char*转CString
#include <iostream>
#include <string>
#include "afx.h"
using namespace std;

int main()
{

char *s = "This is a char* string";
string str = "This is a string";
CString cStr(s);
CString sStr(str.c_str());

getchar();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: