A New Input-Output Package
A new package of IO routines is available.
It was designed with the following goals in mind.
It should be similar in spirit to the earlier Portable
Library, and, to the extent possible, be compatible with it.
At the same time a few dubious design choices
in the Portable Library will be corrected.
It must be as efficient as possible, both in time and in space,
so that there will be no hesitation in using it
no matter how critical the application.
It must be simple to use, and also free of the magic
numbers and mysterious calls the use
of which mars the understandability and portability
of many programs using older packages.
The interface provided should be applicable on all machines,
whether or not the programs which implement it are directly portable
or to machines other than the PDP11 running a version of Unix.
It is intended that this package replace the Portable Library.
Although it is not directly compatible, as discussed below,
it is sufficiently similar that
modifying programs to use it should be a simple exercise.
The most crucial difference between this package and the Portable
Library is that the current offering names streams in terms
of pointers rather than by
the integers known as `file descriptors.'
Thus, for example, the routine which opens a named file
returns a pointer to a certain structure rather than a number;
the routine which reads an open file
takes as an argument the pointer returned from the open call.
Each program using the library must have the line
which defines certain macros and variables.
The library containing the routines is `/usr/lib/libS.a,'
so the command to compile is
All names in the include file intended only for internal use begin
with an underscore `\_' to reduce the possibility
of collision with a user name.
The names intended to be visible outside the package are
The name of the standard input file
The name of the standard output file
The name of the standard error file
is actually \-1, and is the value returned by
the read routines on end-of-file or error.
is a notation for the null pointer, returned by
expands to `struct \_iob' and is a useful
shorthand when declaring pointers
of the size suitable for an IO buffer supplied by the user.
.IP "getc, getchar, putc, putchar, feof, ferror, fileno" 10
Their actions are described below;
to point out that it is not possible to
and that they are not actually functions;
thus, for example, they may not have breakpoints set on them.
The routines in this package, like the Portable
offer the convenience of automatic buffer allocation
and output flushing where appropriate.
Absent, however, is the facility
of changing the default input and output streams
by assigning to `cin' and `cout.'
The names `stdin,' stdout,' and `stderr'
are in effect constants and may not be assigned to.
The routines in the library are in nearly one-to-one
correspondence with those in the Portable Library.
In several cases the name has been changed.
This is an attempt to reduce confusion.
.sr "FILE *fopen(filename, type) char *filename, *type"
opens the file and, if needed, allocates a buffer for it.
is a character string specifying the name.
is a character string (not a single character).
It may be `"r",' `"w",' or `"a"' to indicate
intent to read, write, or append.
The value returned is a file pointer.
If it is NULL the attempt to open failed.
.sr "FILE *freopen(filename, type, ioptr) char *filename, *type; FILE *ioptr
is closed, if necessary, and then reopened
If the attempt to open fails, NULL is returned,
which will now refer to the new file.
Often the reopened stream is
.sr "int getc(ioptr) FILE *ioptr
returns the next character from the stream named by
which is a pointer to a file such as returned by
The integer EOF is returned on end-of-file or when
The null character \(aa\e0\(aa is a legal character.
.sr "int fgetc(ioptr) FILE *ioptr
but is a genuine function,
.sr "putc(c, ioptr) FILE *ioptr
on the output stream named by
which is a value returned from
The character is returned as value,
but EOF is returned on error.
.sr "fputc(c, ioptr) FILE *ioptr
.sr "fclose(ioptr) FILE *ioptr
The file corresponding to
is closed after any buffers are emptied.
A buffer allocated by the IO system is freed.
is automatic on normal termination of the program.
.sr "fflush(ioptr) FILE *ioptr
Any buffered information on the (output) stream named by
Output files are normally buffered
if and only if they are not directed to the terminal,
terminates the process and returns its argument as status
This is a special version of the routine
To terminate without flushing,
.sr "feof(ioptr) FILE *ioptr
returns non-zero when end-of-file
has occurred on the specified input stream.
.sr "ferror(ioptr) FILE *ioptr
returns non-zero when an error has occurred while reading
or writing the named stream.
The error indication lasts until the file has been closed.
.sr "char *gets(s) char *s
reads characters up to a new-line from the standard input.
The new-line character is replaced by a null character.
It is the user's responsibility to make sure that the character array
returns its argument, or NULL if end-of-file or error occurred.
Note that this routine is not compatible with
it is included for downward compatibility.
.sr "char *fgets(s, n, ioptr) char *s; FILE *ioptr
characters from the stream
into the character pointer
The read terminates with a new-line character.
The new-line character is placed in the buffer
followed by a null character.
or NULL if error or end-of-file occurred,
writes the null-terminated string (character array)
it is included for downward compatibility.
.sr "*fputs(s, ioptr) char *s; FILE *ioptr
writes the null-terminated string (character array)
.sr "ungetc(c, ioptr) FILE *ioptr
is pushed back on the input stream named by
Only one character may be pushed back.
.sr "printf(format, a1, . . .) char *format
.sr "fprintf(ioptr, format, a1, . . .) FILE *ioptr; char *format
.sr "sprintf(s, format, a1, . . .)char *s, *format
writes on the standard output.
writes on the named output stream.
puts characters in the character array (string)
The specifications are as described in section
of the Unix Programmer's Manual.
There is a new conversion:
converts a double argument in the style of
.sr "scanf(format, a1, . . .) char *format
.sr "fscanf(ioptr, format, a1, . . .) FILE *ioptr; char *format
.sr "sscanf(s, format, a1, . . .) char *s, *format
reads from the standard input.
reads from the named input stream.
reads from the character string
The specifications are identical
to those of the Portable Library.
reads characters, interprets
them according to a format, and stores the results in its arguments.
each of which must be a pointer,
indicating where the converted input should be stored.
conversion specifications, which are used to direct interpretation
The control string may contain:
Blanks, tabs or newlines, which are ignored.
Ordinary characters (not %) which are expected to match
the next non-space character of the input stream
(where space characters are defined as blank, tab or newline).
Conversion specifications, consisting of the
character %, an optional assignment suppressing character \**,
an optional numerical maximum field width, and a conversion
A conversion specification is used to direct the conversion of the
next input field; the result
is placed in the variable pointed to by the corresponding argument,
unless assignment suppression was
indicated by the \** character.
An input field is defined as a string of non-space characters;
it extends either to the next space character or until the field
width, if specified, is exhausted.
The conversion character indicates the interpretation of the
input field; the corresponding pointer argument must
usually be of a restricted type.
The following conversion characters are legal:
indicates that a single % character is expected
in the input stream at this point;
indicates that a decimal integer is expected
the corresponding argument should be an integer pointer.
indicates that an octal integer is expected in the
input stream; the corresponding argument should be a integer pointer.
indicates that a hexadecimal integer is expected in the input stream;
the corresponding argument should be an integer pointer.
indicates that a character string is expected;
the corresponding argument should be a character pointer
pointing to an array of characters large enough to accept the
string and a terminating `\e0', which will be added.
The input field is terminated by a space character
indicates that a character is expected; the
corresponding argument should be a character pointer;
the next input character is placed at the indicated spot.
The normal skip over space characters is suppressed
to read the next non-space character, try
If a field width is given, the corresponding argument
should refer to a character array, and the
indicated number of characters is read.
indicates that a floating point number is expected in the input stream;
the next field is converted accordingly and stored through the
corresponding argument, which should be a pointer to a
floating point numbers is
possibly containing a decimal point, followed by an optional
exponent field beginning with an E or e followed by an optionally signed integer.
indicates a string not to be delimited by space characters.
The left bracket is followed by a set of characters and a right
bracket; the characters between the brackets define a set
of characters making up the string.
is not circumflex (\|^\|), the input field
is all characters until the first character not in the set between
the brackets; if the first character
after the left bracket is ^, the input field is all characters
until the first character which is in the remaining set of characters
The corresponding argument must point to a character array.
The conversion characters
may be capitalized or preceded
to indicate that a pointer to
Similarly, the conversion characters
to indicate that a pointer to
will function similarly in the future to indicate
int i; float x; char name[50];
scanf( "%d%f%s", &i, &x, name);
int i; float x; char name[50];
scanf("%2d%f%\**d%[1234567890]", &i, &x, name);
and place the string ``56\e0'' in
returns as its value the number of successfully matched and assigned input
This can be used to decide how many input items were found.
On end of file, EOF is returned; note that this is different
from 0, which means that the next input character does not
match what was called for in the control string.
.sr "fread(ptr, sizeof(*ptr), nitems, ioptr) FILE *ioptr
It behaves identically to the Portable Library's
that binary IO is being done is required;
when, for portability reasons,
it becomes required, it will be done
by adding an additional character to the mode-string on the
.sr "fwrite(ptr, sizeof(*ptr), nitems, ioptr) FILE *ioptr
but in the other direction.
.sr "rewind(ioptr) FILE *ioptr
It is not very useful except on input,
since a rewound output file is still open only for output.
.sr "system(string) char *string
is executed by the shell as if typed at the terminal.
.sr "getw(ioptr) FILE *ioptr
returns the next word from the input stream named by
EOF is returned on end-of-file or error,
but since this a perfectly good
.sr "putw(w, ioptr) FILE *ioptr
on the named output stream.
.sr "setbuf(ioptr, buf) FILE *ioptr; char *buf
may be used after a stream has been opened
but before IO has started.
the stream will be unbuffered.
Otherwise the buffer supplied will be used.
It is a character array of sufficient size:
.sr "fileno(ioptr) FILE *ioptr
returns the integer file descriptor associated with the file.
.sr "fseek(ioptr, offset, ptrname) FILE *ioptr; long offset
The location of the next byte in the stream
is 0, the offset is measured from the beginning of the file;
is 1, the offset is measured from the current read or
is 2, the offset is measured from the end of the file.
The routine accounts properly for any buffering.
(When this routine is used on non-Unix systems,
the offset must be a value returned from
and the ptrname must be 0).
.sr "long ftell(ioptr) FILE *ioptr
The byte offset, measured from the beginning of the file,
associated with the named stream is returned.
Any buffering is properly accounted for.
(On non-Unix systems the value of this call is useful only
so as to position the file to the same place it was when
.sr "getpw(uid, buf) char *buf
The password file is searched for the given integer user ID.
If an appropriate line is found, it is copied into
If no line is found corresponding to the user ID
.sr "char *calloc(num, size)
The space is guaranteed to be set to 0 and the pointer is
sufficiently well aligned to be usable for any purpose.
NULL is returned if no space is available.
.sr "cfree(ptr) char *ptr
Space is returned to the pool used by
Disorder can be expected if the pointer was not obtained
The following are macros defined by stdio.h.
returns non-zero if the argument is alphabetic.
returns non-zero if the argument is upper-case alphabetic.
returns non-zero if the argument is lower-case alphabetic.
returns non-zero if the argument is a digit.
returns non-zero if the argument is a spacing character:
tab, new-line, carriage return, vertical tab,
returns the upper-case character corresponding to the lower-case
returns the lower-case character corresponding to the upper-case