65,187
社区成员




int _tmain( int argc, _TCHAR *argv[ ] )
{
setlocale( LC_ALL, "chinese" );
wfstream test;
test.open( "test.txt", ios_base::binary | ios_base::out | ios_base::trunc );
const wchar_t Header[ 2 ] = { 0xFF, 0xFE };
test.write( &Header[ 0 ], 1 );
test.write( &Header[ 1 ], 1 );
const wchar_t A[100] = L"asp晕";
cout << "Length= " << wcslen( A ) << endl;
wcout << A << endl;
test.write( A, sizeof( A ) );
test.close( );
system( "PAUSE" );
return 0;
}
ofstream ofs( "test.txt", ios::binary | ios::out );
ifstream ifs( "Test_Read.txt", ios::binary | ios::in );
wchar_t temp[ 2 ];
ifs.read( (char *)temp, 2 );
memset( temp, L'\0', sizeof(temp) );
ofs.write( "\xFF\xFE", 2 );
int k = 0;
while ( 1 ) {
k ++;
ifs.read( (char *)temp, 2 );
if ( ifs.eof( ) ) break;//经验表明,放在这里最恰当!~
ofs.write( (char *)temp, 2 );
}
ifs.close( );
ofs.close( );
FILE* pFile = NULL;
if(_wfopen_s(&pFile, filename, L"r,ccs=UNICODE") != 0)
return;
...
fclose(pFile);
写只需要把 r 换成 w
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
wfstream wfstrm(stdout);
wfstrm.imbue(locale("chs"));
wfstrm<<L"abc哎"<<endl;
wfstrm.close();
return 0;
}