How to get a FILE pointer from a file descriptor?

前端 未结 3 1414
我寻月下人不归
我寻月下人不归 2020-12-07 14:17

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 t

3条回答
  •  攒了一身酷
    2020-12-07 15:06

    FILE* f = fdopen(d, "w");

    man fdopen output:

    SYNOPSIS

    #include 
    
    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 via fclose(3), fildes is closed also.

提交回复
热议问题