我如何从 wostream 转换为 ostream

发布于 2024-09-01 01:57:15 字数 141 浏览 3 评论 0原文

我正在使用一个接收 ostream 的函数,但我有 wostream 有没有办法将一个函数转换为另一个?

特别是我想使用 boost::write_graphviz ,它需要 ostream 但我目前处于 << wostream 的运算符。

i am using a function that receives ostream but i have wostream is there a way to convert one to the other?

in particular i want to use boost::write_graphviz which takes ostream but i currently in << operator for wostream.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヤ经典坏疍 2024-09-08 01:57:15

我不相信你能转变。你必须获得一个ostream。

I don't believe you can convert. You will have to get an ostream.

暗地喜欢 2024-09-08 01:57:15

如果我是你,我宁愿改变我的方法,而不是尝试将 wostream 转换为 ostream。我确信您想要的东西可以在没有宽字符流的情况下轻松实现。

当然,这取决于您的应用程序的实际目的,但在 10 种情况中有 9 种情况下,简单地实现您自己的转换例程或使用第三方库(例如 iconv)显然就足够了。

更新:(作为我所说内容的证明)
Google 代码只有一个 wofstream 搜索结果和两个 - wifstream 搜索结果。难道这个事实不说明一切吗?

If I were you I'd rather change my approach, rather than trying to convert wostream to ostream. I am sure that what you want to get can easily be achieved without wide char streams.

Of course, it depends on what is the actual purpose of your application, but in 9 of 10 cases simple implemention of your own converting routine or usage of third-party libraries (e.g. iconv) would obviously be enough.

Update: (as a proof of what I'm saying)
Google code has only one search result for wofstream and two - for wifstream. Doesn't that fact speak for itself?

神经大条 2024-09-08 01:57:15

如果图形输出没有 wostream 运算符,那么您可以执行以下操作:



std::wstring convert(std::string const& s)
{
  unsigned char const* tmp = reinterpret_cast<unsigned char const*>(s.data());

  std::wstring res = std::wstring(tmp, tmp + s.size());

  return res;
}

void f(std::wostream & out)
{
  std::ostringstream gout;
  gout << graph; // or whatever...
  out << convert(gout.str());
}

如果您的字符串可以具有高于 177 的任何值,则必须进行强制转换。如果它可以变成奇怪的字符,因为 wstring 构造函数执行的强制转换会将负数变成负数而不是直接位复制。

If the graph output does not have a wostream operator then you can do this:



std::wstring convert(std::string const& s)
{
  unsigned char const* tmp = reinterpret_cast<unsigned char const*>(s.data());

  std::wstring res = std::wstring(tmp, tmp + s.size());

  return res;
}

void f(std::wostream & out)
{
  std::ostringstream gout;
  gout << graph; // or whatever...
  out << convert(gout.str());
}

The cast is necessary if your string can have any value above 177. If it can the turn into strange characters because the cast the wstring constructor will do turns negatives into negatives instead of straight bit copy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文