背景搭建:Win7+VS2010
准备文件:D:\a.txt,文件内容两个汉字:中文 a.txt文件另存格式为UTF-8
VS2010新建C++控制台项目,main函数代码如下
#include <string>
#include <Windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
FILE* fd = fopen("D:\\a.txt", "rb");
char str[20] = "";
fread(str, 1, 20, fd);
fclose(fd);
int size = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
wchar_t *wstr = new wchar_t[size];
MultiByteToWideChar(CP_UTF8, 0 ,str, -1, wstr, size);
wstring wstrs = wstr;
int size2 = WideCharToMultiByte(CP_ACP, 0, wstrs.c_str(), wstrs.size()+1, nullptr, 0, nullptr, nullptr);
char* sstr = new char[size2];
WideCharToMultiByte(CP_ACP, 0, wstrs.c_str(), wstrs.size()+1, sstr, size2, nullptr, nullptr);
delete[] wstr;
delete[] sstr;
return 0;
}
问题:sstr 字符串 最终结果是 ?中文 ,而不是希望的 中文 两字。
求大牛解答,头痛了