问题描述
我在Linux上工作,文件描述符是这个操作系统的主要模型。
I am working on Linux and file descriptors are the main model in this OS.
我想知道是否有任何库或任何方法来检索本地Linux文件描述符,从C ++开始 std :: fstream
。
I was wondering whether is there any library or any way to retrieve the native Linux file descriptor starting from a C++ std::fstream
.
我想到了 boost :: iostream
,因为有一个类 file_descriptor
但我知道它的目的是不同于我想实现的。
I thought about boost::iostream
since there is a class called file_descriptor
but I understood that its purpose is different from the one I want to achieve.
你知道一些方法吗?
推荐答案
您可以采用另一种方式:实现自己的包装文件描述符的流缓冲区,然后使用 iostream
而不是 fstream
。使用可以使任务更简单
You can go the other way: implement your own stream buffer that wraps a file descriptor and then use it with iostream
instead of fstream
. Using Boost.Iostreams can make the task easier.
非便携式gcc解决方案是:
Non-portable gcc solution is:
#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.
}
这篇关于从std :: fstream检索文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!