Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ fopen(3s) — Ultrix-11 3.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

creat(2)

dup(2)

open(2)

pipe(2)

fclose(3s)

fseek(3s)

fopen(3s)

NAME

fopen, freopen, fdopen − open a stream

SYNTAX

#include <stdio.h>

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

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

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

DESCRIPTION

The fopen subroutine opens the file filename, associates a stream with it, and returns a pointer to be used to identify the stream in subsequent operations. 

The type is one of the following values:

r Opens filename for reading. 

w Creates filename for writing. 

a Append: opens for writing at end of file, or creates for writing. 

r+ Opens for update (reading and writing). 

w+ Truncates or creates for update. 

a+ Append; opens or creates for update at end-of-file. 

The freopen subroutine substitutes the named file in place of the open stream.  It returns the original value of stream. The original stream is closed.

The freopen subroutine is typically used to attach the preopened constant names, stdin, stdout, and stderr to specified files. 

The fdopen subroutine associates a stream with a file descriptor.  File descriptors are obtained from open, dup, creat, or pipe, system calls which open files but do not return pointers to a FILE structure stream. Streams are necessary input for many of the 3s library routines.  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 fseek 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. 

RESTRICTIONS

The fdopen subroutine is not portable to systems other than UNIX. 

DIAGNOSTICS

The fopen and freopen subroutines return the pointer NULL if filename cannot be accessed. 

SEE ALSO

creat(2), dup(2), open(2), pipe(2), fclose(3s), fseek(3s)

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