用ofstream/ifstream 读写Unicod的TXT

使用的平台:vs2013 控制台

 

from: http://bbs.csdn.net/topics/360229403

xiayuanzhong:

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( );

  

unicode的TXT使用经验:

1.输出 换行符

  ofstream ofs_c(FILE_NAME_C, ios::binary | ios::out);

  ofs_c.write("\x0D\x00\x0A\x00", 4);// "\r\n"

2.输出空格

  WCHAR_NULL8(ofs) (ofs).write("\x20\x00\x20\x00\x20\x00\x20\x00", 8);//" " x 4

posted @ 2016-09-18 19:03  ourran  阅读(687)  评论(0编辑  收藏  举报