我已经用另一种策略解决了这个问题,output the lines in bytes
. 使用下面的函数输出 wstring,无论它包含什么字符。
void output(ofstream &fout, vector<wstring> wline_list)
{
void outputline(ofstream &, wstring);
//pre output 0xFF and 0xFE to make the file encoding in UTF-16
const BYTE PRE_LOW = 0xFF;
const BYTE PRE_HIGH = 0xFE;
fout << PRE_LOW << PRE_HIGH;
for(vector<wstring>::size_type i(0); i<wline_list.size(); i++)
outputline(fout, wline_list[i]);
}
void outputline(ofstream &fout, wstring line)
{
void getByte(BYTE btchar[2], WORD wdChar);
BYTE btChar[2] = {0,0};
const BYTE CHANGE_LINE1_LOW = 0x0D;
const BYTE CHANGE_LINE1_HIGH = 0x00;
const BYTE CHANGE_LINE2_LOW = 0x0A;
const BYTE CHANGE_LINE2_HIGH = 0x00;
WORD wdChar(0);
for(wstring::size_type i(0); i<line.length(); i++)
{
wdChar = line[i];
getByte(btChar, wdChar);
fout << btChar[0] << btChar[1];
}
//it needs this two chars to change line.
fout << CHANGE_LINE1_LOW << CHANGE_LINE1_HIGH
<< CHANGE_LINE2_LOW << CHANGE_LINE2_HIGH;
}
void getByte(BYTE btchar[2], WORD wdChar)
{
btchar[0] = wdChar % 0x0100;
btchar[1] = wdChar / 0x0100;
}