How to get a FILE pointer from a file descriptor?

BD at Rivenhill picture BD at Rivenhill · Dec 21, 2009 · Viewed 44.5k times · Source

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()?

Answer

Richard Pennington picture Richard Pennington · Dec 21, 2009

Use fdopen():

FILE* fp = fdopen(fd, "w");