Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

creat(2)

dup(2)

open(2)

pipe(2)

fclose(3)

fseek(3)

stdio(3)



  fopen(3)                            CLIX                            fopen(3)



  NAME

    fopen, freopen, fdopen - Opens a stream

  LIBRARY

    Standard C Library (libc.a)

  SYNOPSIS

    #include <stdio.h>

    FILE *fopen(
      char *filename ,
      char *type );

    FILE *freopen(
      char *filename ,
      char *type ,
      FILE *stream; );

    FILE *fdopen(
      int fildes ,
      char *type );

  PARAMETERS

    filename   Points to a filename.

    type       Indicates the manner in which a file is opened.

    stream     Points to a stream.

    fildes     Specifies a file descriptor.

  DESCRIPTION

    The fopen() function opens the file named by filename and associates a
    stream with it.  The fopen() function returns a pointer to the structure
    associated with the stream.

    The filename parameter points to a character string that contains the name
    of the file to be opened.

    The type is a character string having one of the following values:

    r    Open for reading.

    w    Truncate or create for writing.

    a    Append; open for writing at end-of-file, or create for writing.



  2/94 - Intergraph Corporation                                              1






  fopen(3)                            CLIX                            fopen(3)



    r+   Open for update (reading and writing).

    w+   Truncate or create for update.

    a+   Append; open or create for update at end-of-file.

    The freopen() function substitutes the named file in place of the open
    stream.  The original stream is closed, regardless of whether the open
    ultimately succeeds.  The freopen() function returns a pointer to the
    structure associated with stream.

    The freopen() function is typically used to attach the preopened streams
    associated with stdin, stdout and stderr to other files.

    The fdopen() function associates a stream with a file descriptor.  File
    descriptors are obtained from open(), dup(), creat(), or pipe(), which
    open files but do not return pointers to a structure stream.  Streams are
    necessary input for many of the Section 3 library functions.  The type of
    stream must agree with the mode of the open file.

    When a file is opened for update, both input and output may be done on the
    resulting stream.  However, output may not be directly followed by input
    without an intervening fseek() or rewind(), and input may not be directly
    followed by output without an intervening fseek(), rewind(), or an input
    operation which encounters end-of-file.

    When a file is opened for append (that is, when type is ``a'' or ``a+''),
    it is impossible to overwrite information already in the file.  The seek()
    function may be used to reposition the file pointer to any position in the
    file, but when output is written to the file, the current file pointer is
    disregarded.  All output is written at the end of the file and causes the
    file pointer to be repositioned at the end of the output.  If two separate
    processes open the same file for append, each process may write freely to
    the file without fear of destroying output being written by the other.
    The output from the two processes will be intermixed in the file in the
    order in which it is written.

  EXAMPLES

    1.  To open a file for reading:

        FILE *infile;
        infile=fopen("/doc/allen/data","r");


    2.  To associate stdin with a file:

        FILE *infile
        infile=freopen("input/data","r",stdin);





  2                                              Intergraph Corporation - 2/94






  fopen(3)                            CLIX                            fopen(3)



    3.  To associate a file descriptor with a stream:

        int filedescriptor;
        FILE *inputstream;
        if((filedescriptor=open(inputfile,0)) == -1)
                syserr("open");
        inputstream=fdopen(filedescriptor,"r");


  RETURN VALUES

    The fopen(), fdopen(), and freopen() functions return a NULL pointer on
    failure.

  RELATED INFORMATION

    Functions:  creat(2), dup(2), open(2), pipe(2), fclose(3), fseek(3),
    stdio(3)




































  2/94 - Intergraph Corporation                                              3




Typewritten Software • bear@typewritten.org • Edmonds, WA 98026