How to get a FILE pointer from a file descriptor and how to get a file descriptor from a FILE pointer in C on Linux?

I would like to open files by open() directly. But how to get a FILE pointer from a file descriptor in C on Linux? And how to do the reverse direction: get a file descriptor from a FILE pointer?

asked Sep 30, 2015 by anonymous

1 Answer

Get a FILE pointer from a file descriptor (e.g. fd) in C on Linux:

FILE *file = fdopen(fd, "w");

Here, the second parameter is the modes which you can choose those for fopen.

More details can be found in the man page of fdopen: http://www.pkill.info/linux/man/3p-fdopen/ .

Get the file descriptor from a FILE pointer (e.g. file) in C on Linux:

int fd = fileno(file);

More details can be found in the man page of fileno: http://www.pkill.info/linux/man/3p-fileno/ .

answered Oct 1, 2015 by Eric Z Ma (44,280 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...