我可以使用 FILE* 来初始化 C++ ostream 对象?
我可以使用其他地方打开的
FILE* f = ...
东西来初始化某些 std::ostream
实例吗?像这样(伪代码):
FILE *f = ...;
std::ostream os;
os./*bind_to_f*/( f ); // HOW?
os << "Hello world" << std::endl;
Can I use an elsewhere opened
FILE* f = ...
thing to initialize some std::ostream
instance? Like this (pseudocode):
FILE *f = ...;
std::ostream os;
os./*bind_to_f*/( f ); // HOW?
os << "Hello world" << std::endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
FILE*
和流分别是 C 和 C++ 风格的 I/O 功能。由于 C++ 库并不总是了解底层 C 库的实现,因此无法做到这一点。另外考虑一下,如果你能做到这一点,所有权语义会是什么?FILE*
and streams are C and C++ style I/O capabilities respectively. Since the C++ library doesn't always know about the implementation of the underlying C library there's no way to do this. Additionally consider, what would the ownership semantics be if you could do this?使用 GCC,您只需将现有文件描述符传递给构造函数(ref ):
With GCC you can just pass the existing file descriptor to the constructor (ref):
如果您要创建一个在内部使用 FILE* 的 Streambuf 类,您可以做到。例如, HP OpenVMS 似乎有一个类似的类那。
If you were to create a streambuf class that used a FILE* internally, you could do it. For example, HP OpenVMS seems to have a class like that.