How to get a FILE pointer from a file descriptor?

蓝咒 提交于 2019-11-28 15:56:48
Richard Pennington

Use fdopen():

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

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

There is no standard way of doing this (or the reverse) as the C Standard has nothing to say about file descriptors. Your specific platform may or may not provide such a mechanism.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!