使用1.使用memcpy函数转换
CString cstrFilePath;
char charfilepath[256];
memcpy(charfilepath, LPCTSTR(cstrFilePath), cstrFilePath.GetLength() * sizeof(TCHAR));
这种方法理论上是可行的,但是我在MFC默认项目属性下使用此方法转换造成路径读取混乱,具体原因未知,以下是我调试过程中的char数组中间值:
2.通过strncpy_s函数
CString cstrFilePath;
char charfilepath[256];
//以下两步是先将CString转为string,再由c_str()转为const char*
string stra = CStringA(cstrFilePath);
strncpy_s(charfilepath, _countof(charfilepath), stra.c_str(), cstrFilePath.GetLength());
使用此函数成功了,以下是正常中间值: