How to open an ifstream from a file descriptor?

Callat picture Callat · Apr 14, 2017 · Viewed 7.4k times · Source

I am trying to implement an encryption on a file through s3fs using rc4. Basically I need to take a file descriptor that s3fs gives use it to extract the name of the file and then do the thing. So how would I do that in c++?

I don't have any code to show because I don't know how to start it would look something like:

void foo(int fd){
// something

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

Is this even doable? Or am I just doomed to use system calls?

Answer

Ehab Elnabarawy picture Ehab Elnabarawy · Apr 14, 2017

Short answer: you can't, not in standard C++ anyway. There is no official/portable way to do it.

You can, however, find some "work-around" solutions in these previous posts:

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