如何从文件描述符打开 ifstream?

How to open an ifstream from a file descriptor?

我正在尝试使用 rc4 通过 s3fs 对文件进行加密。基本上我需要使用 s3fs 提供的文件描述符来提取文件名,然后执行操作。那么我将如何在 C++ 中做到这一点?

我没有任何代码可以显示,因为我不知道如何开始它看起来像:

void foo(int fd){
// something

string temp; 
temp = // result of that somethingthing;
ifstream in;
in.open(temp,ios::binary | ios::in);
}

这可行吗?还是我注定要使用系统调用?

简短回答: 你不能,反正在标准 C++ 中不行。没有 official/portable 方法。

可以,但是,在这些以前的帖子中找到一些"work-around"解决方案:

How to construct a c++ fstream from a POSIX file descriptor?

Getting a FILE* from a std::fstream

Retrieving file descriptor from a std::fstream