Linux C:如何从文件描述符更改为 FILE 结构,反之亦然?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4210966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:01:20  来源:igfitidea点击:

C : how can I change from file descriptor to FILE struct and vice versa?

clinux

提问by trrrrrrm

Is there any way to change an intfile descriptor to a FILEstruct pointer or/and change FILE*to a file descriptor in C?

有没有办法将int文件描述符更改为FILE结构指针或/和更改FILE*为 C 中的文件描述符?

采纳答案by Kamal

The function fdopen()returns a new (FILE *) associated with an open file descriptor.

该函数fdopen()返回一个与打开的文件描述符相关联的新 (FILE *)。

The function fileno()returns the file descriptor associated with an open FILE *.

该函数fileno()返回与打开的 FILE * 关联的文件描述符。

回答by user172818

Use "fileno()" for FILE->int.

对 FILE->int使用“ fileno()”。

Use "fdopen()" for int->FILE.

对 int->FILE使用“ fdopen()”。