我如何从 wostream 转换为 ostream
我正在使用一个接收 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不相信你能转变。你必须获得一个ostream。
I don't believe you can convert. You will have to get an ostream.
如果我是你,我宁愿改变我的方法,而不是尝试将
wostream
转换为ostream
。我确信您想要的东西可以在没有宽字符流的情况下轻松实现。当然,这取决于您的应用程序的实际目的,但在 10 种情况中有 9 种情况下,简单地实现您自己的转换例程或使用第三方库(例如
iconv
)显然就足够了。更新:(作为我所说内容的证明)
Google 代码只有一个
wofstream
搜索结果和两个 -wifstream
搜索结果。难道这个事实不说明一切吗?If I were you I'd rather change my approach, rather than trying to convert
wostream
toostream
. 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 - forwifstream
. Doesn't that fact speak for itself?如果图形输出没有 wostream 运算符,那么您可以执行以下操作:
如果您的字符串可以具有高于 177 的任何值,则必须进行强制转换。如果它可以变成奇怪的字符,因为 wstring 构造函数执行的强制转换会将负数变成负数而不是直接位复制。
If the graph output does not have a wostream operator then you can do this:
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.