Case one: C++ stream file read string generated by various uncertain coding problems

wstring strFilePath;
strFilePath = strResourcePath + _T("lang\\") + FindFileData.cFileName;

ifstream OpenFile(strFilePath);
if(OpenFile.fail()) continue;
Copy the code

The solution is not to use encoding conversions that can easily go wrong when reading stream files

Case 2: Coding specific transformation

wstring CSettingCenter::UTF8ToGB(const char * strText) { string result; WCHAR *strSrc; LPSTR szRes; Int I = MultiByteToWideChar(CP_UTF8, 0, strText, -1, NULL, 0); strSrc = new WCHAR[i + 1]; MultiByteToWideChar(CP_UTF8, 0, strText, -1, strSrc, i); I = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL); szRes = new CHAR[i + 1]; WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL); result = szRes; delete[]strSrc; delete[]szRes; return (CA2W)result.c_str(); }Copy the code

This method conversion process is more complicated

Case 3: The best solution CA2WEX<>(sep, CP_UTF8) turn

 

strFilePath = CPaintManagerUI::GetResourcePath() + _T("lang\\") + FindFileData.cFileName; HANDLE hFile = ::CreateFileW(strFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { break; } DWORD dwSize = ::GetFileSize(hFile, NULL); if (! dwSize) { CloseHandle(hFile); break; } pLanguageData = new CHAR[dwSize + 1]; DWORD dwRead = 0; ::ReadFile(hFile, pLanguageData, dwSize, &dwRead, NULL); dwLanguageDataSize = dwRead; CloseHandle(hFile); vector<wstring> vctText; vctTemp.clear(); if (! pLanguageData) return; char* next_start = pLanguageData; char* start = next_start; char* end = strchr(start, '\n'); if (end) { *end = 0; } char* sep = strchr(start, '='); if (! sep) { continue; } *sep = 0; sep++; wstring value = CA2WEX<>(sep, CP_UTF8);Copy the code

Case 4 use C language to open FILE fopen, or MFC CFile method to open

 

 

Shenzhen C++ exchange group 876027056

Question consultation wechat 18926447914, can be pulled into the wechat Shenzhen programmer exchange group, dACHun direct HR recruitment internal promotion, as well as various gods