1.5
Character Input and Output
We are now going to consider a
family of related programs for processing character data. You will find that
many programs are just expanded versions of the prototypes that we discuss
here.
The model of input and output
supported by the standard library is very simple. Text input or output,
regardless of where it originates or where it goes to, is dealt with as streams
of characters. A
text stream is a
sequence of characters divided into lines: each line consists of zero or more
characters followed by a newline character. It is the responsibility of the
library to make each input or output stream conform to this model; the C
programmer using the library need not worry about how lines are represented
outside the program.
The standard library provides
several functions for reading or writing one character at a time, of which getchar and putchar are
the simplest. Each time it is called, getchar reads the next input character from a text stream and returns that
as its value. That is, after
c = getchar()
the variable c contains the next character of input. The characters
normally come from the keyboard; input from files is discussed in Chapter 7 The
function putchar prints a character each time it is called:
putchar(c)
prints the contents of the integer variable c as a character. usually on
the screen. Calls to putchar and printf
may be interleaved the output will appear in the order in which the calls are
made.
3.4
Switch
The switch statement is a multi-way
decision that tests whether an expression matches one of a number of constant
integer values, and branches accordingly.
switch (expression) {
case const-expr: statements
case const-expr: statements
default statements
]
Each case is labeled by one or more
integer-valued constants or constant expressions. If a case matches the
expression value, execution starts at that case. All case expressions must be
different. The case labeled default is executed if none of the other cases are
satisfied. A default is optional; if it isn't there and if none of the cases
match, no action at all takes place. Cases and the default clause can occur in
any order.
In Chapter 1 we wrote a program to
count the occurrences of each digit. white space, and all other characters,
using a sequence of if ... else if else. Here is the same program with a switch:
#include <stdio.h>
main() /* count digits, white space, others*/ {
int c, nwhite, nother, ndigit[10];
nwhite = nother = 0:
for (i = 0; i < 10; i++)
ndigit[i] = 0::
while ((c = getchar() != EOF) {
switch (c) {
case '0', case
“1’: case '2': case ‘3’: case ‘4’:
case '5':: case
'6': case '7': case ‘8’: case ‘9’:
ndigit[c-'0']++.
break;
case ‘ ‘:
case ‘\t’:
case ‘\n’:
nwhite++;
break;
default:
nother++;
break
}
}
printf("digits =”);
for (i = 0; i
< 10; i++)
printf(" %d", ndigit[i]):
printf(", white space = %d,
other = %d\n", nwhite, nother):
return 0;
}
The break statement causes an
immediate exit from the switch. Because
cases serve just as labels, after the code for one case is done, execution
falls through to the next unless you take explicit action to escape. break and
return are the most common ways to leave a switch A break statement can also be
used to force an immediate exit from while, for. and do loops, as will be
discussed later in this chapter.
Falling through cases is a mixed
blessing. On the positive side, it allows several cases to be attached to a
single action, as with the digits in this example. But it also implies that
normally each case must end with a break to prevent falling through to the
next. Falling through from one case to another is not robust, being prone to
disintegration when the program is modified. With the exception of multiple
labels or a single computation. fall-throughs should be used sparingly, and
commented.
As a matter of good form, put a break
after the last case (the default here) even though its logically unnecessary.
Some day when another case gets added at the end, this bit of defensive
programming will save you.
7.5 File
Access
The examples so far have all read the
standard input and written the standard output, which are automatically defined
for a program by the local operating system.
The next step is to write a program
that accesses a file that is not already connected to the program. One program
that illustrates the need for such operations is cat, which concatenates a set
of named files onto the standard output. cat is used for printing files on the
screen. and as a general-purpose input collector for programs that do not have
the capability of accessing files by name. For example, the command
cat x.c y.c
prints the contents of the files x.c and y.c (and nothing else) on the
standard output.
The question is how to arrange for
the named files to be read — that is. how to connect the external names that a
user thinks of to the statements that read the data.
The rules are simple. Before it can
be read or written, a file has to be opened by the library function fopen.
fopen takes an external name like x.c or y.c. does some housekeeping and
negotiation with the operating system (details of which needn't concern us),
and returns a pointer to be used in subsequent reads or writes of the file.
This pointer, called the file
pointer. points to a structure that contains information about the file, such
as the location of a buffer, the current character position in the buffer,
whether the file is being read or written, and whether errors or end of file
have occurred. Users don't need to know the details. because the definitions
obtained from <stdio.h> include a structure declaration called FILE. The
only declaration needed for a file pointer is exemplified by
FILE *fp;
FILE *fopen(char
*na , char *mode);
This says that fp is a pointer to a
FILE, and fopen returns a pointer to a FILE. Notice that FILE is a type name,
like int, not a structure tag; it is defined with a typedef. (Details of how fopen
can be implemented on the UNIX System are given in Section 8.5.)
The call to fopen in a program is
fp = fopen(name,
mode);
The first argument of fopen is a
character string containing the name of the file. The second argument is the
mode, also a character string, which indicates how one intends to use the file.
Allowable modes include read ("r"). write ("w"). and append
("a"). Some systems distinguish between text and binary files: for
the latter, a "b" must be appended to the mode string.
If a file that does not exist is
opened for writing or appending. it is created if possible. Opening an existing
file for writing causes the old contents to be discarded, while opening for
appending preserves them. Trying to read a file that does not exist is an
error, and there may be other causes of error as well. like trying to read a
file when you don't have permission. If there is any error, fopen will return
NULL. (The error can be identified more precisely; see the discussion of
error-handling functions at the end of Section I in Appendix B.)
The next thing needed is a way to
read or write the file once it is open. There are several possibilities, of
which getc and putc are the simplest. getc returns the next character from a
file; it needs the file pointer to tell it which file.
int getc(FILE
*fp)
getc returns the next character from the stream referred to by fp: it
returns EOF for end of file or error.
putc is an output function:
int putc(int c,
FILE •fp)
putc writes the character c to the file fp and returns the character
written, or EOF if an error occurs. Like getchar and putchar, getc and putc may
be macros instead of functions.
When a C program is started. the
operating system environment is responsible for opening three files and
providing file pointers for them. These files are the standard input, the
standard output, and the standard error; the corresponding file pointers are
called stdin, stdout, and stderr, and are declared in <stdio.h>. Normally
stdin is connected to the keyboard and stdout and stderr are connected to the
screen, but stdin and stdout may be redirected to files or pipes as described
in Section 7.1.
getchar and putchar can be defined in terms of getc, putc, stdin. and
stdout as follows:
*define getchar() getc(stdin) *define putchar(c) putc((c), stdout)
For formatted input or output of
files, the functions fscanf and fprintf may be used. These are identical to scant
and printf, except that the first argument is a file pointer that specifies the
file to be read or written; the format string is the second argument.
int fscanf(FILE
*fp, char *format, int fprintf(FILE •fp, char *format,
With these preliminaries out of the
way, we are now in a position to write the program cat to concatenate files. The design
is one that has been found convenient for many programs. If there are
command-line arguments, they arc interpreted as filenames, and processed in
order. If there are no arguments. the standard input is processed.
#include <stdio.h.
/* cat:
concatenate files, version 1 */ main(int argc, char *argv[])
{
FILE +fp;
void
filecopy(FILE *, FILE *);
if (argc == 1)
/* no args; copy standard input */ filecopy(stdin, stdout)
else
while (--argc 01
if ((fp =
fopen(*++argv, "r")) == NULL) printf("cat: can't open
%s\n". *argv); return 1,
] else {
filecopy(fp,
stdout);
fclose(fp);
return 0;
/* filecopy:
copy file ifp to file ofp void filecopy(FILE *ifp, FILE .ofp)
int c;
while ((c =
getc(ifp)) != EOF) putc(c, ofp);
The file pointers stdin and stdout are objects of type FILE *. They are constants,
however, not variables. so it is not possible to assign to them. The function
int fclose(FILE
*fp)
is the inverse of fopen; it breaks the connection between the file
pointer: the external name that was established by fopen, freeing the file
pointer another file. Since most operating systems have some limit on the
number files that a program may have open simultaneously. it's a good idea to
free pointers when they are no longer needed, as we did in cat. There is another reason for fclose on an output file
— it flushes the buffer in which putc is collecting output. fclose is called
automatically for each open when a program terminates normally. (You can close
stdin and stdoul they are not needed. They can also be reassigned by the
library function freopen.)
Conventionally. a return value of 0
signals that all is well; non-zero values usually signal abnormal situations. exit
calls fclose for each open output file. to flush out any buffered output.
Within main. return expr is
equivalent to exit(expr). exit has the advantage that it can called from other
functions. and that calls to it can be found with a pattern-searching program
like those in Chapter 5.
The function ferror returns non-zero if an error occurred on the stream f
p.
int ferror(FILE
*fp)
Although output errors are rare.
they do occur (for example, if a disk fills up). so a production program should
check this as well.
The function feof (FILE * ) is analogous to ferror: it returns non-zero
if end of File has occurred on the specified file.
int feof(FILE
•fp)
We have generally not worried about
exit status in our small illustrative pro. grams, but any serious program
should take care to return sensible, useful status values.
7.7 Line
Input and Output
The standard library provides an
input routine fgets that is similar to the getline function that we have used
in earlier chapters:
char •fgets(char
*line, int maxline, FILE *fp)
fgets reads the next input line (including the newline) from file fp into
the character array line; at most maxline-1 characters will be read. The resulting
line is terminated with '\0'. Normally fgets returns line; on end of file or
error it returns NULL. (Our getline returns the line length, which is a more
useful value; zero means end of file.)
For output. the function fputs
writes a string (which need not contain a newline) to a film
int fputs(char
*line, FILE •fp)
It returns EOF if an error occurs,
and zero otherwise_
The library functions gets and puts
arc similar to fgets and fputs, but operate on stdin and stdout Confusingly, gets
deletes the terminal '\n'. and puts adds it.
To show that there is nothing
special about functions like fgets and fputs. here they are, copied from the
standard library on our system:
/* fgets: get at
most n chars from iop */
char •fgets(char
•s, int n, FILE •iop) {
register int c;
register char *cs;
while (- -n >
0 && (c = getc(iop)) != EOF)
if ((*cs++ = c)
== '\n') break;
*cs = '\0';
return (c == EOF
&& cs == s) ? NULL : s, )
}
/* fputs: put
string s on file iop •/
int fputs(char
*s, FILE *iop); {
int c;
while( c = •s++)
putc(c, iop);
return
ferror(iop) ? EOF:0;
For no obvious reason, the standard specifies different return values for
ferror and fputs.
It is easy to implement our getline from fgets:
/* getline: read
a line, return length */
int getline(char
*line, int max) (
if (fgets(line,
max, stdin) == NULL) return 0:
else return
strlen(line);
}
Exercise 7-6. Write a program to compare two files, printing the first
line where they differ.
Exercise 7-7. Modify the pattern finding program of Chapter 5 to take its
input from a set of named files or, if no files are named as arguments, from
the standard input. Should the file name be printed when a matching line is
found?
Exercise 7-8. Write a program to print a set of files, starting each new
one on a new page. with a title and a running page count for each file.
7.8.6
Mathematical Functions
There are more than twenty
mathematical functions declared in <math.h>. here are some of the more
frequently used. Each takes one or two double arguments and returns a double.
sin(x) sine of x, x in radians
cos(x) cosine of x. x in radians
atan2(y,x) arctangent of x/y
in radians
exp(x) exponential function
log(x) natural (base e) logarithm of x (x
>0)
log10(x) common (base l0) logarithm of x (x>0)
pow(x,y)
sqrt (x) square root of x (x>=0)
fabs(x) absolute value of x
7.8.7
Random Number Generation
The function rand( ) computes a
sequence of pseudo-random integers in the range zero to RAND MAX, which is
defined in <stdlib.h>. One way to pro-duce random floating-point numbers
greater than or equal to zero but less than one is
*define frand()
((double) rand() / (RAND_ MAX+1))
(If your library already provides a
function for floating-point random numbers. it is likely to have better
statistical properties than this one.)
The function srand(unsigned) sets
the seed for rand. The portable implementation of rand and srand suggested by
the standard appears in Section 2.7.
Exercise 7-9. Functions like isupper
can be implemented to save space or to save time. Explore both possibilities.