0

在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);
4

2 回答 2

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";
于 2015-06-17T20:38:37.263 回答
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();
于 2015-06-18T16:55:25.267 回答