对于下面的代码,是否可以将结果输出为字符串而不是wofstream?谢谢你!
wstring w = L"test";
std::wofstream ofs("test.txt");
std::locale utf8_locale(std::locale(), new boost::archive::detail::utf8_codecvt_facet());
ofs.imbue(utf8_locale);
std::copy(w.begin(),w.end(),
std::ostream_iterator<wchar_t, wchar_t>(ofs));发布于 2012-09-06 10:58:13
输出字符串流是一个C++特性,其行为类似于输出文件流,因为它们都继承自std::ostream,这意味着您可以在两者中使用几乎相同的函数。但是,字符串流对字符串而不是文件进行操作。
这意味着您需要做的就是(因为您还没有使用任何wofstream-specific函数)将ofs的类型更改为字符串流:
#include <sstream>
std::wostringstream oss;现在我还不能构建语言环境的东西(我到目前为止还没有用过它们),但是注释它们确实产生了正确的结果(see this test)。如您所见,您可以通过字符串流的str()函数访问该字符串。
https://stackoverflow.com/questions/12292187
复制相似问题