Does fopen create a file descriptor?
Asked Answered
B

2

10

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

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 fcntl() when there is no "fd" to use as input.

Bordiuk answered 9/3, 2012 at 23:37 Comment(0)
P
19

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

Potluck answered 9/3, 2012 at 23:40 Comment(0)
C
1

Since glibc 2.7 you can add e character directly to mode string. man 3 fopen:

e (since glibc 2.7) Open the file with the O_CLOEXEC flag. See open(2) for more information. This flag is ignored for fdopen().

Catkin answered 5/12, 2023 at 22:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.