What is the difference between fopen and open in C

Discussion in 'C Programming' started by magicman, May 12, 2008.

  1. magicman

    magicman Guest

    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
    magicman, May 12, 2008
    #1
    1. Advertising

  2. magicman

    Lew Pitcher Guest

    In comp.lang.c, magicman wrote:

    > 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. ------
    Lew Pitcher, May 12, 2008
    #2
    1. Advertising

  3. magicman

    santosh Guest

    magicman wrote:

    > 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 <news:comp.unix.programmer> should be more
    appropriate.
    santosh, May 12, 2008
    #3
  4. In article <>,
    magicman <> wrote:

    >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
    --
    :wq
    Richard Tobin, May 12, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,143
  2. Nonee
    Replies:
    2
    Views:
    2,651
    Neredbojias
    Oct 25, 2005
  3. Grocery Clerk

    fopen() vs. open()

    Grocery Clerk, Sep 30, 2004, in forum: C Programming
    Replies:
    10
    Views:
    977
    Dan Pop
    Oct 1, 2004
  4. Jack

    open() and fopen()

    Jack, Jul 10, 2006, in forum: C Programming
    Replies:
    27
    Views:
    1,195
    Michael Wojcik
    Jul 16, 2006
  5. Michel Rouzic
    Replies:
    4
    Views:
    1,833
    Michel Rouzic
    Apr 28, 2008
Loading...

Share This Page