[英]wstring_buffer not giving output
The following program is originally from Josuttis' book "The C++ Standard Library", 2nd Ed, pg 903.以下程序最初来自 Josuttis 的书“The C++ Standard Library”,第 2 版,第 903 页。
http://coliru.stacked-crooked.com/a/cab87d0d7d9f8a7b http://coliru.stacked-crooked.com/a/cab87d0d7d9f8a7b
/**
An input stream reads UTF-8 multibyte characters into wide characters.
Then, an output stream writes these wide characters as UTF-16
multibyte characters with leading byte order marks.
*/
#include <string>
#include <iostream> /// cout
#include <locale> /// wbuffer_convert
#include <codecvt> /// codecvt_utf8<T>>
using namespace std;
int main()
{
// create input stream reading UTF-8 sequences:
wbuffer_convert<codecvt_utf8<wchar_t>>
utf8inBuf(cin.rdbuf());
wistream utf8in(&utf8inBuf);
// create output stream writing UTF-16 sequences:
wbuffer_convert<codecvt_utf16<wchar_t,
0xFFFF,
generate_header>>
utf16outBuf(cout.rdbuf());
wostream utf16out(&utf16outBuf);
// write each character read:
wchar_t c;
while (utf8in.get(c))
{
utf16out.put(c);
}
cout << "Completed!" << endl;
}
Given the following input:给定以下输入:
Hello, World!
the program doesn't print any corresponding output.该程序不打印任何相应的输出。
Why is this happening?为什么会这样? How do I get the program to work?
我如何让程序运行?
I made this work on Ubuntu.我在 Ubuntu 上完成了这项工作。 I had to add this line at the very beginning, right after
main
:我必须在一开始就在
main
之后添加这一行:
std::cin.sync_with_stdio(false);
otherwise the utf8inBuf
buffer wasn't filled from cin
.否则
utf8inBuf
缓冲区不会从cin
填充。
And flush the output stream with:并使用以下命令刷新输出流:
utf16out << flush;
at the very end, after the while
loop.最后,在
while
循环之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.