0

Using unicode compile in vs2008 How do you output many language characters to a file in C++ with wofstream?

I can do it in C code no problem e.g.

 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");
 }

when I open the file it's all correct, but with below C++ program all I get is the first line and I have tried locale::global(locale("")); with same result.

 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();

and I have tried inserting this with same result:-

  std::locale mylocale(""); 
  MyOutputStream.imbue(mylocale);
user3725395
  • 145
  • 2
  • 13

2 Answers2

1

worked it out... here it is:-

    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";
user3725395
  • 145
  • 2
  • 13
  • You should not need to supply your own buffer. But outputting a `0xFEFF` character (Unicode codepoint `U+FEFF WIDTH NO-BREAK SPACE`) outputs a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark) at the front of the file to help text editors know the file is Unicode encoded. You could also use `MyOutputStream << L"\xFEFF";` for that instead of `MyOutputStream.put(0xFEFF)`. – Remy Lebeau Jun 17 '15 at 22:09
  • well one would have thought so, but I found trying that I get the no output issue. – user3725395 Jun 18 '15 at 16:48
  • So I took out just the: MyOutputStream.put(0xFEFF); and it still works/opens correctly in notepad, notepad++. but without the buffer it doesn't write anything out. I did notice by opening the C program output txt file that the FEFF wasn't in the file at all. – user3725395 Jun 18 '15 at 16:50
  • the final code that works is:- 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(); – user3725395 Jun 18 '15 at 16:54
0

the final code that works is:-

  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();
user3725395
  • 145
  • 2
  • 13