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)  —  Subroutines

NAME

fopen, freopen, fdopen − Open a stream

LIBRARY

Standard C Library (libc.a)

SYNOPSIS

#include <stdio.h>

FILE ∗fopen(
        const char ∗path,
        const char ∗mode);

FILE ∗fdopen(
int filedes,
const char ∗mode);

FILE ∗freopen(
const char ∗path,
const char ∗mode,
FILE ∗stream);

PARAMETERS

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

modePoints to a character string that controls whether the file is opened for reading (r), writing (w), or appending (a) and whether the file is opened for updating (+). 

streamSpecifies the input stream. 

filedesSpecifies a valid open file descriptor. 

DESCRIPTION

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

The mode parameter controls the access allowed to the stream. The parameter can have one of the following values:

rOpens the file for reading. 

wCreates a new file for writing, or opens and truncates a file to zero length.  (The file is not truncated under the fdopen() function.) 

aAppends (opens a file for writing at the end of the file, or creates a file for writing). 

r+Opens a file for update (reading and writing). 

w+Truncates or creates a file for update.  (The file is not truncated under the fdopen() function.) 

a+Appends (opens a file for update, writing at the end of the file, or creates a file for writing). 

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() function call or a file positioning operation (fseek(), fsetpos(), or rewind() 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 mode parameter is a or a+), it is impossible to overwrite information already in the file. You can use the fseek() 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. 

If two separate processes open the same file for append, each process can write freely to the file without destroying the output being written by the other. The output from the two processes is intermixed in the order in which it is written to the file. Note that if the data is buffered, it is not actually written until it is flushed. 

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

If the mode parameter 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 mode parameter 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 parameter. 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(), dup(), creat(), or pipe() 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 mode of the stream specified must agree with the mode of the open file. 

NOTES

The fopen() function does not distinguish between text and binary files. Specifying a value of mode that consists of an r, a w, or an a followed by a b indicates a binary file. However, the function ignores the b in the value of mode. 

AES Support Level:
Full use.

RETURN VALUES

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

ERRORS

If any of the following conditions occurs, the fopen(), fdopen(), and freopen() functions set errno to the value that corresponds to the condition. 

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

[EBADF]The filedes parameter is not a valid file descriptor (fdopen() only). 

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

[EINVAL]The mode parameter is not a valid mode. 

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

[ELOOP]Too many links were encountered in translating path. 

[EMFILE]The 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 parameter points to an empty string. 

[ENOMEM]Insufficient space to allocate a buffer. 

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

[ENOTDIR]A component of the pathname 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. 

[EOPNOTSUPP]
The named file is a socket bound to the file system (a UNIX domain socket) and cannot be opened.

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

[ETXTBSY]The file is being executed and the mode requires write access. 

RELATED INFORMATION

Functions: open(2), fclose(3), fseek(3), setbuf(3). 

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