c++unicodewofstream

Type of UTF-16 encoding, using wofstream in Windows


Recently, I want to write a text file in unicode (UTF-16) under Windows.

By refering to http://www.codeproject.com/KB/stl/upgradingstlappstounicode.aspx, here is the code I am applying.

When I use Notepad to open up the document, here is the display. Newline seems disappear!!!

alt text
(source: google.com)

When I use Firefox with UTF-16 encoding selected, here is the display.

alt text
(source: google.com)

I try to open under JEdit, using the following encoding

  1. UTF-16 - Nope. Rubbish display.
  2. UTF-16BE - Nope. Rubbish display.
  3. UTF-16LE - Fine. Able to show multiple lines.

My guess is that, I need to provide additional byte ordering information? But how?

My objective is to get this UTF-16 document able to display well under Notepad, as my customer just love to use Notepad.

P/S PLEASE! Don't ever suggest me using UTF-8. Thank you.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <locale>
#include <windows.h>
#include <tchar.h>
// For StringCchLengthW.
#include <Strsafe.h>
#include <cassert>

using namespace std;

// appearing in the NullCodecvtBase typedef.
using std::codecvt ; 
typedef codecvt < wchar_t , char , mbstate_t > NullCodecvtBase ;

class NullCodecvt
    : public NullCodecvtBase
{

public:
    typedef wchar_t _E ;
    typedef char _To ;
    typedef mbstate_t _St ;

    explicit NullCodecvt( size_t _R=0 ) : NullCodecvtBase(_R) { }

protected:
    virtual result do_in( _St& _State ,
                   const _To* _F1 , const _To* _L1 , const _To*& _Mid1 ,
                   _E* F2 , _E* _L2 , _E*& _Mid2
                   ) const
    {
        return noconv ;
    }
    virtual result do_out( _St& _State ,
                   const _E* _F1 , const _E* _L1 , const _E*& _Mid1 ,
                   _To* F2, _E* _L2 , _To*& _Mid2
                   ) const
    {
        return noconv ;
    }
    virtual result do_unshift( _St& _State , 
            _To* _F2 , _To* _L2 , _To*& _Mid2 ) const
    {
        return noconv ;
     }
    virtual int do_length( _St& _State , const _To* _F1 , 
           const _To* _L1 , size_t _N2 ) const _THROW0()
    {
        return (_N2 < (size_t)(_L1 - _F1)) ? _N2 : _L1 - _F1 ;
    }
    virtual bool do_always_noconv() const _THROW0()
    {
        return true ;
    }
    virtual int do_max_length() const _THROW0()
    {
        return 2 ;
    }
    virtual int do_encoding() const _THROW0()
    {
        return 2 ;
    }
} ;

#define IMBUE_NULL_CODECVT( outputFile ) \
{ \
(outputFile).imbue( std::locale(locale::classic(), new NullCodecvt )) ; \
} 

int main()
{
    std::wofstream file;
    IMBUE_NULL_CODECVT( file ) ;
    file.open(L"C:\\可以爱我吗.TXT", ios::out | ios::binary);
    file << L"ABC" << std::endl;
    file << L"我爱你" << std::endl;
    file << L"Bye bye" << std::endl;

    printf("done\n");
    getchar();
}

Solution

  • When you do ios::binary, the file is opened in binary mode which means newlines aren't converted to the correct \r\n encoding on Windows.

    If you write "\r\n" instead of std::endl, it should work in Notepad. I'm not sure if this is the best solution.