cunixfopenfile-descriptor

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.


Solution

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