用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'', sizeof(temp) );
ofs.write( "xFFxFE", 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("x0Dx00x0Ax00", 4);// " "

2.输出空格

  WCHAR_NULL8(ofs) (ofs).write("x20x00x20x00x20x00x20x00", 8);//" " x 4

如果问题解决起来不妥或者有更好的解决办法,麻烦请告知,帮助曾经和你一样的入门者,谢谢。
原文地址:https://www.cnblogs.com/ourran/p/5882739.html