可能的重复:
从 std::fstream 获取 FILE* https://stackoverflow.com/questions/109449/getting-a-file-from-a-stdfstream
我正在 Linux 上工作,文件描述符是该操作系统中的主要模型。
我想知道是否有任何库或任何方法可以从 C++ 开始检索本机 Linux 文件描述符std::fstream
.
我想过boost::iostream
因为有一个类叫做file_descriptor
但我明白它的目的和我想要达到的目的不同。
你知道有什么方法可以做到这一点吗?
您可以采用另一种方式:实现您自己的包装文件描述符的流缓冲区,然后将其与iostream
代替fstream
. Using Boost.Iostreams http://www.boost.org/doc/libs/1_50_0/libs/iostreams/doc/index.html可以使任务变得更容易。
非便携式 gcc 解决方案是:
#include <ext/stdio_filebuf.h>
{
int fd = ...;
__gnu_cxx::stdio_filebuf<char> fd_file_buf{fd, std::ios_base::out | std::ios_base::binary};
std::ostream fd_stream{&fd_file_buf};
// Write into fd_stream.
// ...
// Flushes the stream and closes fd at scope exit.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)