如何将包含不同语言的wstring行写入文件
How to write a wstring line contains different language to a file?
我从22个不同语言的文件中获得了单独的部分,并将它们制作成一个wstring行,如:
wstring wstr_line = L""IDS_TOAST_ECOON","eco Mode is turned On.","ecoモードをオンにしました。","Režim eco je zapnutý.","Økoindstillingen er aktiveret"..."
我使用wofstream将wstr_line放入一个文件中,但这一行在日语部分结束(\"ecoモードをオンにしました。\")。如果我设置wfout.imbue("chs");
,则线路在捷克部分完成(\"Režim eco je zapnutý\")
如何将这一行正确地写入文件?
试着把它作为代码中的第一行:
int main()
{
std::cout.imbue(std::locale(""));
这将应用程序的本地设置为机器支持的内容(对于宽字符串,可能是UTF-32)。不幸的是,对于程序员来说,默认的local是"C",而"C"local的codecvt方面没有做任何有用的事情(可能会在没有转换的情况下将宽宪章截断为单个字节)。
我已经用另一种策略output the lines in bytes
解决了这个问题。使用下面的函数来输出wstring,无论它包含什么chracter。
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;
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- TinyXml:从XML文件中读取wstring
- 如何附加 wstring 并将其写入文件
- 如何将包含不同语言的wstring行写入文件
- 为什么std::wofstream不将所有wstring打印到文件中
- Boost错误:Boost.文件系统V3及更高版本需要std::wstring支持
- C++:将文件内容读取到wstring中
- 读取Unicode UTF-32文件到wstring
- 按字节顺序读取UTF-16格式的文件到wstring
- 如何使用wstring重命名文件
- 如何使用wfstream将UTF-16文件读取到wstring中