Abr*_*ile 15 c++ fstream file-descriptor
可能重复:
从std :: fstream获取文件*
我正在研究Linux,文件描述符是这个操作系统的主要模型.
我想知道是否有任何库或任何方法从C++开始检索本机Linux文件描述符std::fstream.
我想过,boost::iostream因为有一个叫做的课,file_descriptor但我明白它的目的与我想要达到的目的不同.
你知道某种方法吗?
您可以采用另一种方式:实现自己的包含文件描述符的流缓冲区,然后使用它iostream而不是fstream.使用Boost.Iostream可以使任务更容易.
非便携式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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19955 次 |
| 最近记录: |