Jon Kump
Jon Kump

Reputation: 604

Does fopen create a file descriptor?

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.

Upvotes: 10

Views: 4349

Answers (2)

dimich
dimich

Reputation: 1843

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

Upvotes: 1

cnicutar
cnicutar

Reputation: 182619

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

Upvotes: 19

Related Questions