读书人

已定义UNICODE为何输出还是乱码?求

发布时间: 2012-05-10 16:02:39 作者: rapoo

已定义UNICODE,为何输出还是乱码?求解,谢谢!

C/C++ code
#include <iostream>#include <fstream>#include <cstdlib>#include <string>using namespace std;#define INFILE    "MyData.txt"#define OUTFILE "MyData.txt"int main(int, char **, char **){    wifstream ifs(INFILE, ios::in);    if (ifs)    {        cerr << "Error: " << INFILE<< "already exists" << endl;        exit(1);    }    wofstream ofs(OUTFILE, ios::out);    if (!ofs)    {        cerr << "Error: unable to write to " << OUTFILE << endl;        exit(2);    }    const wstring strStuff = L"新概念英语与习题解答";    const wchar_t* pStr = L"3: const char * ";    ofs << L"1: A literal string" << endl;    ofs.write(pStr, wcslen(pStr));    ofs << endl;    ofs << strStuff << endl;}


[解决办法]

ofs.imbue(std::locale("chs"));
[解决办法]
wofstream ofs(OUTFILE, ios::out);
后面加入如下语句:
ofs.imbue(std::locale( "chs"));
就能解决乱码问题。
[解决办法]
http://support.microsoft.com/kb/q274012/

读书人网 >C++

热点推荐