1.CString转int
例如:
CString str=_T("10");
int num=0;
num=_tstoi(str);
则num为10.
2.int转CString
例如:
CString s;
char ch[20];
int num=15;
itoa(num,ch,10);
s=ch;
则s为"15";
3.char []转CString
例如:
CString s;
char ch[]="hello world";
s=ch;
则s为"hello world"
4.char *转CString
例如:
CString str=_T("您好");
char *infor;
int strLength=str.GetLength()+1;
infor=new char[strLength];
strncpy(infor,str,strLength);
则infor为"您好",当然在这里用完之后记得delete[] infor,否则内存泄漏就不好了
这里得说一下MFC配置里面最好选择使用多字节字符集,不然上面有些转换可能不成功
PS:这个我郁闷了一下午才发现...
5.MFC下文件读取
例如:
CFile file(_T("帅哥计算器.txt"),CFile::modeRead);
char *pBuf;
DWORD dwFileLen;
dwFileLen=file.GetLength();
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;
file.Read(pBuf,dwFileLen);
file.Close();
则pBuf里面内容为帅哥计算器.txt文件内容,记得delete[] pBuf.
6.MFC下文件保存
例如:
CFile file(_T("我的文件.txt"),CFile::modeCreate | CFile::modeWrite);
char infor[]="hello world";
file.Write(infor,strlen(infor));
file.Close();
这里保存模式可以自行选择,我用的是CFile::modeCreate | CFile::modeWrite.
最后想说一下有件奇怪的事,也感激一下PanBook.
就是我有个Place的类,加int x;成员,想写个void setX(int x);函数,但是在Place.cpp中用this->居然没有x出来...这个问题实在不好百度,结果咨询PanBook(相当给力),得知解决方法如下:
把VS2010关了,然后删除ncb文件,重新打开VS,好了...如图