276

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?

3
  • 12
    fopen is part of standard C library, open is not. Use fopen when writing portable code.
    – Aziz
    Nov 1, 2009 at 21:53
  • 9
    @Aziz, open is a POSIX function though.
    – dreamlax
    Nov 1, 2009 at 21:55
  • If you have a file descriptor created by pipe() — or perhaps socket(), though that's less likely — and you want to use standard FILE * I/O on it, then you need to use fdopen() to create the stream. There are also numerous options that can be specified with open() that cannot be specified with fopen() — perhaps most prominently O_EXCL, but there are certainly others too — and those might be good reasons for using open() (instead of fopen()) followed by fdopen(). Mar 21, 2019 at 4:24

11 Answers 11

293

Of those, fdopen is not like the others. fdopen is what you would use if you first called open and then wanted a FILE *. There is no sense doing that if you have the choice to just call fopen instead. There are cases where you won't be able to get fopen to do exactly what you want, but they're outside the scope of this question.

So, let's just pretend fdopen isn't even there and get on with the question.

There are four main reasons to use fopen instead of open.

  1. fopen provides you with buffering IO that may turn out to be a lot faster than what you're doing with open.
  2. 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 (though the world appears to be converging on LF-only (except IETF text-based networking protocols like SMTP and HTTP and such)).
  3. A FILE * gives you the ability to use fscanf and other stdio functions.
  4. Your code may someday need to be ported to some other platform that only supports ANSI C and does not support the open function.

In my opinion (and experience) 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 or fflush at the appropriate times.

If you're doing seeks (aka fsetpos or fseek 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.

11
  • Almost any program that does not stream IO, where you end up doing seeks into the middle of the file. I should update my response to clarify. Nov 1, 2009 at 22:08
  • 11
    To clarify when buffering gets in the way. It's when you use seek. The following read with whatever command (fgets, fgetc, fscanf, fread) , will always read the whole size of the buffer (4K, 8K or whatever you set). By using the direct I/O you can avoid that. In that case it's even better to use pread instead of a seek/read pair (1 syscall instead of 2). May 5, 2010 at 13:16
  • 3
    Handling interrupted read() and write() calls is a convenient fifth reason to use the libc family of functions.
    – nccc
    Aug 26, 2012 at 21:54
  • 4
    @m-ric: Well, that's a somewhat unrelated question, but yes. All platforms that support ioctl also support the fileno call which takes a FILE * and returns a number that can be used in an ioctl call. Be careful though. FILE * related calls may interact surprisingly with using ioctl to change something about the underlying file descriptor. Sep 28, 2012 at 15:53
  • 2
    I own a map rendering library (CartoType) which does its own buffering. I experimented with moving from high-level to low-level io (from fopen to open, etc.) and benchmarked the performance before and after. Low-level io was better, but only by 3%. I have not yet decided whether a 3% improvement is worth it. Probably yes, because I can encapsulate the platform-dependent code. Apr 12, 2015 at 15:45
76

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

5
  • 6
    Are there any disadvantages to using the buffered 'f...' versions of open?
    – LJM
    Nov 1, 2009 at 21:58
  • 10
    @L. Moser, yes, when you are already buffering the data, and hence the additional buffer adds unnecessary copying and memory overhead. Nov 2, 2009 at 1:41
  • 11
    actually there are other disadvantages. fopen() does not provide the same level of control when opening files, for example create permissions, sharing modes, and more. typically open() and variants provide much more control, close to what the operating system actually provides Nov 7, 2009 at 11:54
  • 5
    There are also the extreme cases where you mmap the file and do changes with normal I/O (as incredible as it sounds we do actually that in our project and for real good reasons), buffering would be in the way. May 5, 2010 at 13:20
  • You also may want to use other system functions, like using open() for preloading files into page cache via readahead(). I guess rule of thumb is "use fopen unless you absolutely need open()", open() actually lets you do fancy stuff (setting/not setting O_ATIME and similar). May 14, 2013 at 4:11
65

fopen vs open in C

  1. fopen is a library function while open is a system call.

  2. fopen provides buffered IO which may be faster compared to open which is non-buffered.

  3. fopen is portable while open not portable (open is environment specific).

  4. fopen returns a pointer to a FILE structure (FILE *) while open returns an integer that identifies the file.

  5. A FILE * gives you the ability to use fscanf and other stdio.h functions.

6
  • 15
    open is a POSIX standard, so it's quite portable
    – osvein
    Oct 5, 2017 at 13:22
  • fopen() is not async-signal-safe (none of the buffered io is), therefore it should not be used in any signal handler - or a function that may be called by it and be taken with care if used in functions that use asynchronous threads. Using open() adds complexity and misses the buffered-io, ... Jun 16, 2021 at 18:47
  • @osvein Not really portable anymore. Works fine with Linuxes, but is now deprecated in Windows. You are supposed to use _open, _close now. Sort of a minor point. I guess the headers needed on Linux and Windows are also different. I think it would be hard to write portable code without some "#if OS_LINUX" lines in it.
    – Jiminion
    May 2, 2022 at 14:40
  • @Jiminion Works fine with Linuxes, but is now deprecated in Windows. Microsoft also quite risibly "deprecates" fopen(). And strcpy(). Along with probably half the required functions in C. You are supposed to use _open, _close now. No, you're not "supposed to". "Microsoft wants you to because it's not portable" is probably closer to the mark. Mar 13 at 2:08
  • 5
    open is a system call. Ugh. I don't know why this meaningless artificial demarcation exists. open() on just about every extant POSIX system is a library function exactly like fopen() is. The open() library function is usually implemented around the openat system call. And even then, before there was an openat() system call, open() was a library function wrapped around the open() system call anyway. fopen() is just a bigger library function wrapper around the same system call that the open() library function uses. Mar 13 at 15:34
20

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 use fopen. As far as fdopen 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.

1
  • I still use open because I dislike the const char *mode instead of int mode Mar 10 at 17:43
12

If you have a FILE *, you can use functions like fscanf, fprintf and fgets etc. If you have just the file descriptor, you have limited (but likely faster) input and output routines read, write etc.

9

open() is a system call and specific to Unix-based systems and it returns a file descriptor. You can write to a file descriptor using write() which is another system call.
fopen() is an ANSI C function call which returns a file pointer and it is portable to other OSes. We can write to a file pointer using fprintf.

In Unix:
You can get a file pointer from the file descriptor using:

fP = fdopen(fD, "a");

You can get a file descriptor from the file pointer using:

fD = fileno (fP);
8

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

while (retval = close(fd), retval == -1 && ernno == EINTR) ;
3
  • 5
    For close, this depends on the operating system. It's incorrect to do the loop on Linux, AIX and some other operating systems.
    – strcat
    Dec 30, 2013 at 22:29
  • 1
    In addition, using read and write will suffer the same issue, i.e., they can get interrupted by a signal before processing the input/output entirely and the programmer has to handle such situations, while fread and fwrite handle signal interruptions nicely.
    – Marcelo
    Jul 5, 2018 at 20:55
  • I've seen this way too many times, but never a good reason why this would ever be issue. Maybe you could provide an example where it would actually be a necessity to not use SA_RESTART in your signal handler and instead have syscalls return EINTR?
    – user11877195
    Mar 10, 2022 at 16:37
6

I changed to open() from fopen() for my application, because fopen was causing double reads every time I ran fopen fgetc . Double reads were disruptive of what I was trying to accomplish. open() just seems to do what you ask of it.

3

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

3

Depends also on what flags are required to open. With respect to usage for writing and reading (and portability) f* should be used, as argued above.

But if basically want to specify more than standard flags (like rw and append flags), you will have to use a platform specific API (like POSIX open) or a library that abstracts these details. The C-standard does not have any such flags.

For example you might want to open a file, only if it exits. If you don't specify the create flag the file must exist. If you add exclusive to create, it will only create the file if it does not exist. There are many more.

For example on Linux systems there is a LED interface exposed through sysfs. It exposes the brightness of the led through a file. Writing or reading a number as a string ranging from 0-255. Of course you don't want to create that file and only write to it if it exists. The cool thing now: Use fdopen to read/write this file using the standard calls.

-1

Opening a file using fopen

Before we can read(or write) information from (to) a file on disk, we must open the file. To open the file, we call the function fopen().

  1. fopen() searches the disk for the file to be opened.
  2. Then, it loads the file from disk into a place in memory called the buffer.
  3. Finally, it sets up a character (char) pointer that points to the first character of the buffer.

Sometimes, during the buffering process, fopen() may time out.

So, when comparing the fopen() library call (high-level i/o) to the open() (low-level i/o) system call, it is faster and more appropriate to use open() rather than fopen().

2
  • is open faster than fopen?
    – obayhan
    Dec 7, 2016 at 7:48
  • yes ,open is the system call, which is faster than fopen - comparitively @obayhan
    – prashad
    Apr 11, 2017 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.