Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Thanks.

like image 365
Jon Kump Avatar asked Mar 09 '12 23:03

Jon Kump


People also ask

How is a file descriptor created?

File descriptors are an index into a file-descriptor table stored by the kernel. The kernel creates a file-descriptor in response to an open call and associates the file-descriptor with some abstraction of an underlying file-like object; be that an actual hardware device, or a file-system or something else entirely.

What does the function fopen () do?

The fopen() function opens the file specified by filename and associates a stream with it. The mode variable is a character string specifying the type of access requested for the file. The mode variable contains one positional parameter followed by optional keyword parameters.

Does fopen append create a new file?

a. Open a text file in append mode for writing at the end of the file. The fopen() function creates the file if it does not exist and is not a logical file.

What is the use of fopen () function in C?

The fopen() method in C is a library function that is used to open a file to perform various operations which include reading, writing etc. along with various modes.


1 Answers

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

like image 111
cnicutar Avatar answered Sep 27 '22 19:09

cnicutar