最近想在Windows下用unicode(UTF-16)写一个文本文件。
通过参考http://www.codeproject.com/KB/stl/upgradingstlappstounicode.aspx,这是我正在应用的代码。
当我使用记事本打开文档时,这里是显示。换行符似乎消失了!!!
(来源:google.com)
当我使用 Firefox 并选择了 UTF-16 编码时,这里是显示。
(来源:google.com)
我尝试在JEdit下打开,使用如下编码
- UTF-16 - 不。垃圾陈列。
- UTF-16BE - 不。垃圾陈列。
- UTF-16LE - 很好。能够显示多行。
我的猜测是,我需要提供额外的字节排序信息吗?但是怎么做?
我的目标是让这个 UTF-16 文档能够在记事本下很好地显示,因为我的客户只是喜欢使用记事本。
P/S 请!永远不要建议我使用 UTF-8。谢谢你。
#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();
}