在vs2008中使用unicode编译 如何使用wofstream将多个语言字符输出到C ++文件中?
我可以用C代码做到这一点没问题,例如
FILE *out;
if( (out = _wfopen( L"test.txt", L"wb" )) != NULL )
{
fwprintf(out,L"test\r\n");
fwprintf(out,L"наказание\r\n");
fwprintf(out,L"ウェブ全体から検索\r\n");
}
当我打开文件时,它都是正确的,但是使用以下C ++程序,我得到的只是第一行,我尝试过locale :: global(locale(""));结果相同。
wofstream MyOutputStream(L"test.txt");
if(!MyOutputStream)
{
AfxMessageBox(L"Error opening file");
return;
}
MyOutputStream << L"test\r\n";
MyOutputStream << L"наказание\r\n";
MyOutputStream << L"ウェブ全体から検索\r\n";
MyOutputStream.close();
我尝试插入相同的结果: -
std::locale mylocale("");
MyOutputStream.imbue(mylocale);
答案 0 :(得分:1)
解决了......这里是: -
wofstream MyOutputStream(L"c:\\test2.txt", ios_base::binary);
wchar_t buffer1[128];
MyOutputStream.rdbuf()->pubsetbuf(buffer1, 128);
MyOutputStream.put(0xFEFF);
MyOutputStream << L"test\r\n";
MyOutputStream << L"наказание\r\n";
MyOutputStream << L"ウェブ全体から検索\r\n";
答案 1 :(得分:0)
最终的代码是: -
wofstream MyOutputStream(L"c:\\test2.txt", ios_base::binary);
if(!MyOutputStream)
{
AfxMessageBox(L"Error opening file");
return;
}
wchar_t buffer1[128];
MyOutputStream.rdbuf()->pubsetbuf(buffer1, 128);
MyOutputStream << L"test\r\n";
MyOutputStream << L"наказание\r\n";
MyOutputStream << L"ウェブ全体から検索\r\n";
MyOutputStream.close();