如:
读取ini文件的属性值(中文)问乱码
我调用下面的接口,从ini配置文件中读取相关的属性值
CString icon_name;
GetPrivateProfileString(szTypeName,szIconName,"",icon_name.GetBuffer(MAX_PATH),MAX_PATH,strIniPath);
配置文件内容如下:
[devtree]
wendu=111111111
shidu=我爱你侃大山
获取到的shidu就是乱码文章来源:https://www.toymoban.com/news/detail-704934.html
解决方式文章来源地址https://www.toymoban.com/news/detail-704934.html
CString CProfile::GetString(LPCTSTR strSection, LPCTSTR szName, LPCTSTR szDefault)
{
char szBuffer[MAX_PATH];
CString szValue = _T("");
GetPrivateProfileString(strSection, szName, szDefault,
szBuffer, MAX_PATH, m_szIniName);
int iLen = strlen(szBuffer);
if (iLen > 0)
szValue = szBuffer;
string strtest = UTF8ToAnsi(szBuffer);
szValue = strtest.c_str();
return szValue;
}
std::wstring UTF8ToUnicode(const char* strSrc)
{
std::wstring wstrRet;
if (NULL != strSrc)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strSrc, -1, NULL, 0) * sizeof(WCHAR);
WCHAR* strDst = new(std::nothrow) WCHAR[len + 1];
if (NULL != strDst)
{
MultiByteToWideChar(CP_UTF8, 0, strSrc, -1, strDst, len);
wstrRet = strDst;;
delete[]strDst;
}
}
return wstrRet;
}
std::string UnicodeToAnsi(const WCHAR* strSrc)
{
std::string strRet;
if (NULL != strSrc)
{
int len = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
char* strDst = new(std::nothrow) char[len + 1];
if (NULL != strDst)
{
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, strDst, len, NULL, NULL);
strRet = strDst;
delete[]strDst;
}
}
return strRet;
}
std::string UTF8ToAnsi(const char* strSrc)
{
return UnicodeToAnsi(UTF8ToUnicode(strSrc).c_str());
}
到了这里,关于GetPrivateProfileString 中文乱码解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!