UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Thursday January 29, 1998
Assigned Reading:
- A Book on C: 9.1 - 9.6 and 11.7
- Programming Abstractions in C: 2.6
Handouts (available on-line):
Project 1
Topics Covered:
- We compared the functionality of scanf() versus
GetInteger() from the Roberts library. Notice that
GetInteger() will continue prompting the user for input
until the user enters an integer correctly. The scanf() function
does not do this, but does provide enough information so you could do
the error checking yourself. However, this error checking is non-trivial
to accomplish correctly. Part of the reason for the difficulty is that
UNIX/C programs tend to be written with the idea that the program can be
executed without a user sitting at the terminal.
- The scanf() function is an elaborate pattern matching utility:
- You can match strings in the input that are not assigned to
any parameters.
Program and
sample run.
- You can match for strings composed of letters in a specified
set. For example, %10[a-zA-Z] specifies 10 characters of
lower and upper case letters.
Program and sample run.
- Part of the difficulty with scanf() comes from the fact
that when a pattern match fails, the input stream is left at the point
of failure. That is, the next character of the input stream is the
first character where failure is detected. This was illustrated in some
of the previous examples. The next program
shows the difficulty this creates when trying to read in two integers on
two lines. Sample run.
- A naive attempt to continue pestering the user until the user
enters a correct integer can result in an infinite loop.
Program and sample
run.
- One way to fix this problem is to use the getchar()
function to consume the rest of the input line, up to and including the
carriage return. Program and sample run.
- The scanf() and printf() functions have analogs
that work with files called fscanf() and fprintf().
Here we need to use fopen() to open the files for reading or
writing first. The pointer returned by fopen() should be
checked to determine whether the file was successfully opened.
Program and sample
run.
- When we print an integer using the printf() or
fprintf() function, this converts an integer to a string
which is then displayed in the terminal or written out to a file.
In order to write the binary value of the integer to a file, we need
to use the fwrite() function. The next program writes an array of integers to a
file called "binary" (sample run).
Note that because the file is a file of integers and not a file of
text (UNIX lied), you must use a utility such as the "octal dump"
utility od to examine the file.
- The reverse of fwrite() is fread(), which we can
use to read in the integers from the binary file.
Program and sample
run.
- The fwrite() and fread() functions can also be
used to write and read entire structures. You will need to this feature
in Project 1.
- You should also consult man pages
for fwrite() and fread().
Last Modified:
22 Jul 2024 11:27:44 EDT
by
Richard Chang
Back up
to Spring 1998 CMSC 202 Section Homepage