Is there any reason (other than syntactic ones) that you'd want to use
FILE *fdopen(int fd, const char *mode);
or
FILE *fopen(const char *path, const char *mode);
instead of
int open(const char *pathname, int flags, mode_t mode);
when using C in a Linux environment?
open() will be called at the end of each of the fopen() family functions. open() is a system call and fopen() are provided by libraries as a wrapper functions for user easy of use
opening a file using fopen
before we can read(or write) information from (to) a file on a disk we must open the file. to open the file we have called the function fopen.
this the way of behaviour of fopen function
there are some causes while buffering process,it may timedout. so while comparing fopen(high level i/o) to open (low level i/o) system call , and it is a faster more appropriate than fopen.
Unless you're part of the 0.1% of applications where using
open
is an actual performance benefit, there really is no good reason not to usefopen
. As far asfdopen
is concerned, if you aren't playing with file descriptors, you don't need that call.Stick with
fopen
and its family of methods (fwrite
,fread
,fprintf
, et al) and you'll be very satisfied. Just as importantly, other programmers will be satisfied with your code.Using open, read, write means you have to worry about signal interaptions.
If the call was interrupted by a signal handler the functions will return -1 and set errno to EINTR.
So the proper way to close a file would be
open()
is a low-level os call.fdopen()
converts an os-level file descriptor to the higher-level FILE-abstraction of the C language.fopen()
callsopen()
in the background and gives you a FILE-pointer directly.There are several advantages to using FILE-objects rather raw file descriptors, which includes greater ease of usage but also other technical advantages such as built-in buffering. Especially the buffering generally results in a sizeable performance advantage.
First, there is no particularly good reason to use
fdopen
iffopen
is an option andopen
is the other possible choice. You shouldn't have usedopen
to open the file in the first place if you want aFILE *
. So includingfdopen
in that list is incorrect and confusing because it isn't very much like the others. I will now proceed to ignore it because the important distinction here is between a C standardFILE *
and an OS-specific file descriptor.There are four main reasons to use
fopen
instead ofopen
.fopen
provides you with buffering IO that may turn out to be a lot faster than what you're doing withopen
.fopen
does line ending translation if the file is not opened in binary mode, which can be very helpful if your program is ever ported to a non-Unix environment.FILE *
gives you the ability to usefscanf
and other stdio functions.open
function.In my opinion the line ending translation more often gets in your way than helps you, and the parsing of
fscanf
is so weak that you inevitably end up tossing it out in favor of something more useful.And most platforms that support C have an
open
function.That leaves the buffering question. In places where you are mainly reading or writing a file sequentially, the buffering support is really helpful and a big speed improvement. But it can lead to some interesting problems in which data does not end up in the file when you expect it to be there. You have to remember to
fclose
orfflush
at the appropriate times.If you're doing seeks (aka
fsetpos
orfseek
the second of which is slightly trickier to use in a standards compliant way), the usefulness of buffering quickly goes down.Of course, my bias is that I tend to work with sockets a whole lot, and there the fact that you really want to be doing non-blocking IO (which
FILE *
totally fails to support in any reasonable way) with no buffering at all and often have complex parsing requirements really color my perceptions.