保存文件为UTF8格式XML file(Writing UTF-8 files in C++)
来源:互联网 发布:淘宝威龙爱疯能信吗 编辑:程序博客网 时间:2024/05/17 03:26
Let’s say you need to write an XML file with this content:
< ?xml version="1.0" encoding="UTF-8"? >
< root description="this is a naïve example" >
< /root >
How do we write that in C++?
At a first glance, you could be tempted to write it like this:
#include< fstream >
int main()
{
std::ofstream testFile;
testFile.open("demo.xml", std::ios::out| std::ios::binary);
std::string text =
"< ?xml version=\"1.0\" encoding=\"UTF-8\"? >\n"
"< root description=\"this is a naïve example\" >\n< /root >";
testFile << text;
testFile.close();
return0;
}
When you open the file in IE for instance, surprize! It's not rendered correctly:
So you could be tempted to say "let's switch to wstring and wofstream".
int main()
{
std::wofstream testFile;
testFile.open("demo.xml", std::ios::out| std::ios::binary);
std::wstring text =
L"< ?xml version=\"1.0\" encoding=\"UTF-8\"? >\n"
L"< root description=\"this is a naïve example\" >\n< /root >";
testFile << text;
testFile.close();
return0;
}
And when you run it and open the file again, no change. So, where is the problem? Well, the problem is that neither ofstream nor wofstream write the text in a UTF-8 format. If you want the file to really be in UTF-8 format, you have to encode the output buffer in UTF-8. And to do that we can use WideCharToMultiByte(). This Windows API maps a wide character string to a new character string (which is not necessary from a multibyte character set). The first argument indicates the code page. For UTF-8 we need to specify CP_UTF8.
The following helper functions encode a std::wstring into a UTF-8 stream, wrapped into a std::string.
#include< windows.h >
std::string to_utf8(constwchar_t* buffer,int len)
{
int nChars =::WideCharToMultiByte(
CP_UTF8,
0,
buffer,
len,
NULL,
0,
NULL,
NULL);
if(nChars ==0)return"";
string newbuffer;
newbuffer.resize(nChars);
::WideCharToMultiByte(
CP_UTF8,
0,
buffer,
len,
const_cast<char*>(newbuffer.c_str()),
nChars,
NULL,
NULL);
return newbuffer;
}
std::string to_utf8(const std::wstring& str)
{
return to_utf8(str.c_str(),(int)str.size());
}
With that in hand, all you have to do is doing the following changes:
int main()
{
std::ofstream testFile;
testFile.open("demo.xml", std::ios::out| std::ios::binary);
std::wstring text =
L"< ?xml version=\"1.0\" encoding=\"UTF-8\"? >\n"
L"< root description=\"this is a naïve example\" >\n< /root >";
std::string outtext = to_utf8(text);
testFile << outtext;
testFile.close();
return0;
}
And now when you open the file, you get what you wanted in the first place.
And that is all!
- 保存文件为UTF8格式XML file(Writing UTF-8 files in C++)
- 保存文件为utf8格式。
- C++ 保存文件为UTF8编码格式
- CSV文件保存为utf8编码格式
- 让TinyXML保存文件为UTF-8格式
- 让TinyXML保存文件为UTF-8格式
- PHP文件保存出现的BOM头(utf-8)的解决方式——保存格式为ANSI即可
- 62 ----这个文件夹保存的格式为utf8的,否则会出不来;----文件保存格式不对也出不来:
- java创建文件保存为utf-8
- vim 保存utf-8文件为gbk
- 批量转换文件为utf-8格式
- 如何将aspx页面保存为utf-8格式
- editplus怎么设定保存的格式默认为utf-8
- source insight如何保存为utf-8的格式编码
- Writing AVI files in OpenCV
- Writing files in Node.js
- java--解决文件输出流保存内容无中文时文件编码为ANSI格式不是utf-8
- reading and writing xml files
- html()方法和text()方法有区别嘛?
- 中文编码问题-终端和vim
- Leaflet与OpenLayer
- 适配器模式【Adapter Pattern 】
- CDN技术原理
- 保存文件为UTF8格式XML file(Writing UTF-8 files in C++)
- java was started but returned exit code =13
- LeetCode 3Sum 求和 java
- 装了别的插件,SVN插件不见了
- 第三方登录
- [LeetCode]040-Combination Sum II
- CentOS 7部署OpenStack(1)—准备基础环境
- AWK教程
- linux redhat7.0+apache+mysql+PHP配置日志