I'm wondering when to use one function versus the other. I know open is
Unix and fopen is standard C, so I was wondering when one would want to
use open and thus lose portability. I'd imagine there must be some
advantages to open, I was just wondering what they are.
More precisely, open() is in the POSIX standard, while fopen() is
in the C standard.
POSIX "imports" the entire C standard (more or less), so any POSIX
system will have fopen(), but not all ANSI/ISO C systems will have
the POSIX semantics for open(), even if they have open(). (The
comp.unix.programmer newsgroup is suitable for POSIX questions, in
general. That would be the place to ask "what can I do with open()
that I cannot do with fopen(), on a POSIX-conformant system?"
There are a few things, but not many, because POSIX also has
fcntl().)
The advantage to using a narrower standard -- that is, one that
applies to fewer systems -- is that it limits you to fewer systems,
so that you know more about them. If the only machine you *ever*
have to program is the Zorblatt-45, then things that only work on
the Zorblatt-45 are fine. It may be trivial to gronkle the
ribberlatch on the Zorblatt-45, but very difficult on the most
models of the Franikle; but the trivial code is all you need.
The advantage to using a broader standard -- that is, one that
applies to virtually every computer in the world -- is that your
code is broadly applicable. Of course, you cannot gronkle anything
in "pure" ANSI/ISO C. You cannot even draw graphics, or fsync()
files. Picking narrower standards allows you to "do more stuff",
on fewer machines.
Can you achieve your goal using the broad standard that has few
bells and whistles? Will it be significantly easier to code using
the narrow standard that only works on three brands of computer?
Or is there a middle road, in which you decide what functionality
is required above and beyond the broad (but feature-poor) ANSI/ISO
C standard, and write your code so that most of it is completely
portable, and only the "feature-invoking" code requires a rewrite
when moving from Linux to Windows, or MacOS, or Tandem NonStop, or
OS/MVS?