fopen是否创建了文件描述符?

时间:2022-09-08 02:50:23

Looking at the man page for fopen I cannot get a definite answer to this question.

看看fopen的手册页我无法得到这个问题的明确答案。

FILE *fopen(const char *path, const char *mode);

I understand that fopen returns a file pointer to a stream but is a file descriptor created as a byproduct? I am trying to make sure that I include the flag FD_CLOEXEC on every instance a file descriptor is created. If a file descriptor is in fact created from fopen what is the best way to use fnctl() when there is no "fd" to use as input.

我知道fopen返回一个指向流的文件指针,但是作为副产品创建的文件描述符?我试图确保在每个创建文件描述符的实例上包含标志FD_CLOEXEC。如果文件描述符实际上是从fopen创建的,那么当没有“fd”用作输入时,使用fnctl()的最佳方法是什么。

Thanks.

1 个解决方案

#1


16  

On Unix (which I assume you're using because you're mentioning fcntl) it does open a file descriptor, as fopen(3) eventually calls open(2). You can get that file descriptor via fileno(3).

在Unix上(我假设你正在使用,因为你提到了fcntl)它确实打开了一个文件描述符,因为fopen(3)最终调用open(2)。您可以通过fileno(3)获取该文件描述符。

#1


16  

On Unix (which I assume you're using because you're mentioning fcntl) it does open a file descriptor, as fopen(3) eventually calls open(2). You can get that file descriptor via fileno(3).

在Unix上(我假设你正在使用,因为你提到了fcntl)它确实打开了一个文件描述符,因为fopen(3)最终调用open(2)。您可以通过fileno(3)获取该文件描述符。