參考 http://blog.csdn.net/e_wsq/article/details/26307817
參考了網上很多的關於CString->string的方法 如GetBuffer()等 均有問題,後來翻到了上面的文章才解決問題。
這裏把它寫成函數,直接提供使用:
void CString2char(CString cstr,char CharString[])
{
LPCWCHAR origString = cstr;
size_t origsize = wcslen(origString) + 1;
size_t convertedChars = 0;
wcstombs_s(&convertedChars, CharString, origsize, origString, _TRUNCATE);
}
使用:CString cstr = "ABCD";
char carray[100];
CString2char(cstr,carray);
cout << carray << endl;
已正確輸出ABCD