Read-write Unicode (LE) code:
#include <iostream>#include<fstream>#include<locale>#include"TextCodeChange.h"using namespacestd;//#define ENDL L "\ r \ n"intMain () {Locale loc (Locale::classic (),New classnullcodecvt); Wstring Wstr; Wifstream Wfin (L"Test.txt", ios_base::binary); Wofstream Wfout (L"Test1.txt", ios_base::binary);; Wfout.imbue (Loc); wchar_t header[1] = {0xFEFF }; Wfout.write (header,1); Wfin.imbue (Loc); Wcout.imbue (Locale ("")); while(Std::getline (Wfin, wstr))//While (Wfin >> wstr){wcout<< Wstr <<Endl; Wfout<< Wstr <<Endl; } wfin.close (); Wfout.close (); return 0;}
Header file TextCodeChange.h
usingstd::codecvt; typedef CODECVT< wchar_t,Char, mbstate_t >nullcodecvtbase;classNULLCODECVT: Publicnullcodecvtbase{ Public: typedef wchar_t _E; typedefChar_to; typedef mbstate_t _ST; ExplicitNULLCODECVT (size_t _r=0): Nullcodecvtbase (_r) {}protected: VirtualResult Do_in (_st&_state,Const_to* _f1,Const_to* _L1,Const_to*&_mid1, _e* F2, _e* _l2, _e*&_mid2)Const { returnNoconv; } VirtualResult Do_out (_st&_state,Const_e* _f1,Const_e* _L1,Const_e*&_mid1, _to* F2, _e* _l2, _to*&_mid2)Const { returnNoconv; } VirtualResult Do_unshift (_st&_state, _to* _f2, _to* _l2, _to*& _mid2)Const { returnNoconv; } Virtual intDo_length (_st& _state,Const_to*_f1,Const_to* _l1, size_t _n2)Const_throw0 () {return(_n2 < (size_t) (_L1-_f1))? _N2: _L1-_f1; } Virtual BOOLDo_always_noconv ()Const_throw0 () {return true ; } Virtual intDo_max_length ()Const_throw0 () {return 2 ; } Virtual intDo_encoding ()Const_throw0 () {return 2 ; }} ;
C + + stream and internationalization