Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

open(2)

fclose(3)

fseek(3)

setbuf(3)

FOPEN(3)                             SysV                             FOPEN(3)



NAME
     fopen, freopen, fdopen - Opens a stream

SYNOPSIS
     #include <stdio.h>

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

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

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


DESCRIPTION
     The fopen function opens the file named by the path argument and
     associates a stream with it, returning a pointer to the FILE structure of
     this stream.

     path Points to a character string that contains the name of the file to
          be opened. If the final component of the path argument specifies a
          symbolic link, the link is traversed and pathname resolution
          continues.

     type Points to a character string that has one of the following values:

          r    Open file for reading.

          w    Create a new file for writing, or open and truncate to zero
               length.  (The file is not truncated under the fdopen function.)

          a    Append (open file for writing at the end of the file, or create
               for writing).

          rb   Open file for reading.

          wb   Create a file for writing, or open and truncate to zero length.

          ab   Append (open file for update, writing at the end of the file,
               or create for writing).

          r+   Open for update (reading and writing).

          w+   Truncate or create for update.  (The file is not truncated
               under the fdopen function.)

          a+   Append (open file for update, writing at End-of-File, or create
               for writing).

          r+b or rb+
               Open file for update (reading and writing).

          w+b or wb+
               Create file for update, or open and truncate to zero length.

          a+b or ab+
               Append (open a file for update, writing at the end of the file,
               or create for writing).

          The presence or absence of the character "b" in any of the above
          list of mode argument values has no effect.

     stream
          Specifies the input stream.

     filedes
          Specifies a valid open file descriptor.

     When you open a file for update, you can perform both input and output
     operations on the resulting stream. However, an output operation cannot
     be directly followed by an input operation without an intervening
     fflush(3S function call or a file positioning operation (fseek(3S),
     fsetpos(3S), or rewind(3S) function). Also, an input operation cannot be
     directly followed by an output operation without an intervening flush or
     file positioning operation, unless the input operation encounters the end
     of the file.

     When you open a file for append (that is, when the type argument is a or
     a+), it is impossible to overwrite information already in the file. You
     can use the fseek(3S) function to reposition the file pointer to any
     position in the file, but when output is written to the file, the current
     file pointer is ignored. All output is written at the end of the file and
     the file pointer is repositioned to the end of the output.

     When opened, a stream is fully buffered if and only if it can be
     determined not to refer to an interactive device.  The error and End-of-
     File indicators for the stream are cleared.

     The fopen function allocates a file descriptor as open does.  If the type
     argument is w, a, w+, or a+ and the file did not previously exist, upon
     successful completion the fopen function marks the st_atime, st_ctime and
     st_mtime fields of the file and the st_ctime and st_mtime fields of the
     parent directory for update.  If the type argument is w or w+ and the
     file did previously exist, upon successful completion the fopen function
     marks the st_ctime and st_mtime fields of the file for update.

     The freopen function substitutes the named file in place of the open
     stream.  The original stream is closed regardless of whether the open
     function succeeds with the named file. The freopen function returns a
     pointer to the FILE structure associated with the stream argument. 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 obtained
     from an open(2), dup(2), creat(2), or pipe(2) function. These functions
     open files but do not return pointers to FILE structures. Many of the
     standard I/O package functions require pointers to FILE structures. Note
     that the type of stream specified must agree with the mode of the open
     file.

DIAGNOSTICS
     If the fopen, fdopen, or freopen function fails, a null pointer is
     returned and errno is set to indicate the error.

ERRORS
     fopen and freopen fail if one of the following are true:

     [EACCES]       Search permission is denied on a component of the path
                    prefix, or the file exists and the permissions specified
                    by the type argument are denied, or the file does not
                    exist and write permission is denied for the parent
                    directory of the file to be created.

     [EINTR]        The fopen function was interrupted by a signal which was
                    caught.

     [EISDIR]       The named file is a directory and type requires write
                    access.

     [EMFILE]       OPEN_MAX file descriptors are currently open in the
                    calling process.

     [ENAMETOOLONG]
                    The length of the path string exceeds PATH_MAX or a
                    pathname component is longer than NAME_MAX.

     [ENFILE]       Too many files are currently open in the system.

     [ENOENT]       The named file does not exist or the path argument points
                    to an empty string.

     [ENOSPC]       The directory or file system that would contain the new
                    file cannot be expanded.

     [ENOTDIR]      A component of the path prefix is not a directory.

     [ENXIO]        The named file is a character special or block special
                    file and the device associated with this special file does
                    not exist.

     [EROFS]        The named file resides on a read only file system and type
                    requires write access.

     No errors are defined for fdopen

SEE ALSO
     open(2), fclose(3), fseek(3), setbuf(3)

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