用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
如果问题解决起来不妥或者有更好的解决办法,麻烦请告知,帮助曾经和你一样的入门者,谢谢。

浙公网安备 33010602011771号