标签:
读写Unicode(LE)代码:
#include <iostream> #include <fstream> #include <locale> #include "TextCodeChange.h" using namespace std; //#define endl L"\r\n" int main() { locale loc(locale::classic(), new class NullCodecvt); 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; }
头文件TextCodeChange.h
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 ; } } ;
标签:
原文地址:http://www.cnblogs.com/elitiwin/p/4248167.html