What is the difference between fopen and open in C

M

magicman

Is difference lies in the fact that fopen part of c library and
platform in-depended, whereas open is a system call? what about
functionalities?

thx
 
L

Lew Pitcher

Is difference lies in the fact that fopen part of c library and
platform in-depended, whereas open is a system call? what about
functionalities?

With respect to the C standard, fopen() is defined by the standard, takes
specific arguments, performs a specific function, and returns specific
results, while open() is *not* defined (or even recognized) by the
standard. fopen() is guaranteed to be part of the standard I/O library in
a hosted environment, while open() is left available as any sort of user
function.

In /some environments/, open() is an environment-specific function that
provides low-level ("system") access to specific I/O functions. However,
there is no guarantee (from the C language pov) of what open() does, what
arguments it takes or what it returns, and it is perfectly legal for an
application program to include a function called open() with it's own code.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
S

santosh

magicman said:
Is difference lies in the fact that fopen part of c library and
platform in-depended, whereas open is a system call?

Basically yes.
what about functionalities?

Which also unsurprisingly differ. Generally open is more flexible than
fopen. It allows you to specify various status values for the file and
returns error codes, which fopen is not guaranteed to do.

See man 3 open and man 2 open. The actual system call is wrapped by a
user-space function, both with identical names.

To further discuss open <should be more
appropriate.
 
R

Richard Tobin

magicman said:
Is difference lies in the fact that fopen part of c library and
platform in-depended, whereas open is a system call? what about
functionalities?

open() is the Unix system call for opening files. The fact that it's
a system call rather than some other kind library function isn't very
important to users; but it corresponds to the fact that in Unix it's
the basic file opening method. Some other operating systems have a
similar function, but as C comes from the world of Unix you probably
mean that one.

fopen() is the standard C function for opening files. A Unix
implementation will use open() in the implementation of fopen(),
but the stream returned by fopen() provides buffering and works
with functions like printf().

Unless you want to take advantage of system-specific features, stick
with fopen().

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top