I'm playing around with mkstemp(), which provides a file descriptor, but I want to generate formatted output via fprintf(). Is there an easy way to transform the file descriptor provided by mkstemp() into a FILE * structure that is suitable for use with fprintf()?
How to get a FILE pointer from a file descriptor?
59.2k views Asked by BD at Rivenhill At
3
There are 3 answers
2
On
FILE* f = fdopen(d, "w");
man fdopen output:
SYNOPSIS
#include <stdio.h>
FILE *
fdopen(int fildes, const char *mode);
The
fdopen()function associates a stream with the existing file descriptor,fildes. The mode of the stream must be compatible with the mode of the file descriptor. When the stream is closed viafclose(3),fildesis closed also.
Use
fdopen():