We can get a file descriptor from a FILE* stream by using the fileno()
function. Is there a function for getting a FILE* stream from the file descriptor without reopening the file?
FILE *fdopen(int fd, const char *mode);
See fdopen(3)
, but it should be on the same page as fopen(3)
:
The
fdopen()
function associates a stream with the existing file descriptor, fd. The mode of the stream (one of the values"r"
,"r+"
,"w"
,"w+"
,"a"
,"a+"
) must be compatible with the mode of the file descriptor. The file position indicator of the new stream is set to that belonging to fd, and the error and end-of-file indicators are cleared. Modes"w"
or"w+"
do not cause truncation of the file. The file descriptor is notdup
’ed, and will be closed when the stream created byfdopen()
is closed. The result of applyingfdopen()
to a shared memory object is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With