How to write a string wstring contains a different language for the file?

I got split parts from 22 files in different languages ​​and made them a wstring string, for example:

wstring wstr_line = L"\"IDS_TOAST_ECOON\",\"eco Mode is turned On.\",\"ecoモードをオンにしました。\",\"Režim eco je zapnutý.\",\"Økoindstillingen er aktiveret\"..." 

I used wofstream to place wstr_line in the file, but the line was finished in the Japanese part ("eco モ ー ド を オ ン に し ま し た."). If I set wfout.imbue("chs"); , the line ended in the Czech part (\ "Režim eco je zapnutý. \")

How to write this line to a file?

+4
source share
2 answers

Try using this as the first line in your code:

 int main() { std::cout.imbue(std::locale("")); 

This installs the local application according to what the machine supports (which is probably UTF-32 for wide characters). Unfortunately, the local default is “C” for programmers, and the codecvt facet for the local “C” does nothing useful (perhaps trimming wide charters to one byte without conversion).

0
source

I solved the problem in another strategy, output the lines in bytes . Use the function below to print wstring no matter what it contains.

 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; } 
0
source

All Articles