最近、WindowsでテキストファイルをUnicode(UTF-16)で書きたいと思っています。
http://www.codeproject.com/KB/stl/upgradingstlappstounicode.aspxを参照すると、ここに適用するコードがあります。
メモ帳を使ってドキュメントを開くと、次のように表示されます。改行が消えるようです!!!
(出典: google.com )
UTF-16 エンコーディングを選択して Firefox を使用すると、次のように表示されます。
(出典: google.com )
次のエンコーディングを使用して、JEditで開こうとしました
- UTF-16 - いいえ。ゴミ表示。
- UTF-16BE - いいえ。ゴミ表示。
- UTF-16LE - 結構です。複数の行を表示できます。
私の推測では、追加のバイト順情報を提供する必要がありますか? しかし、どのように?
私の目的は、この UTF-16 ドキュメントをメモ帳の下で適切に表示できるようにすることです。私の顧客はメモ帳を使用するのが大好きです。
追伸お願いします!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();
}