如何从文件描述符获取FILE *流? [英] How to get a FILE* stream from a file descriptor?

查看:190
本文介绍了如何从文件描述符获取FILE *流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用fileno()函数从FILE *流中获取文件描述符.有没有从文件描述符中获取FILE *流而无需重新打开文件的功能?

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);

请参阅fdopen(3),但它应与fopen(3)在同一页上:

See fdopen(3), but it should be on the same page as fopen(3):

fdopen()函数将流与现有流相关联 文件描述符 fd .流的模式(值之一 "r""r+""w""w+""a""a+")必须兼容 使用文件描述符的模式.文件位置指示器 新流的设置为属于 fd 的流,并且错误和 文件结束指示符被清除.模式"w""w+"不 导致文件被截断.文件描述符未dup, 当fdopen()创建的流为 关闭.将fdopen()应用于共享内存对象的结果 是未定义的.

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 not dup’ed, and will be closed when the stream created by fdopen() is closed. The result of applying fdopen() to a shared memory object is undefined.

这篇关于如何从文件描述符获取FILE *流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆