DAY29
Wednesday, 18 July 2012
Today we might talk about:
-
Using putchar() to print one character at a time (printable.c);
-
Using getchar() and putchar() to read one character at a time from the terminal
and write out one character at a time (type.c);
-
Using getchar() and putchar() to read one character at a time from a file
and write out one character at a time (type.c with "<" operator);
-
Using scanf() to read words one at a time. (read_words.c)
-
Using fgets() to read lines (or parts of lines) at a time. (read_chunks.c
and read_lines.c)
-
Character by character operations: counting characters, capitalizing
characters, replacing a character by another.
-
Word by word operations: does a certain word appear in a text?
How many times does it occur?
-
Line by line operations: how many records (lines) are there in a text?
Copy a text, but print it so that each line starts with '// ', that is,
make it a set of C comments.
Programs and functions we might talk about:
-
capital.c capitalizes a lowercase character.
-
lower.c lowercases a capital character.
-
p10.5.c,
a program which asks the user to enter text, and reads three words of that text.
-
printable.c uses putchar() to print
characters 0 through 127...but prints a '*' if the character isn't
printable.
-
read_chunks.c uses fgets() to read
data into a string of a given length. Data will be read in until
the end of the line, or the string is filled up. Surprisingly,
if the input line wasn't completely read, the next call to fgets()
will pick up where the previous call stopped. This means you can
read an entire file, 20 characters at a time, without missing any.
-
read_lines.c uses fgets() to read
data into a string of length LINE_MAX, a special value defined in
<limits.h>. No input line may be longer than this value,
so this program is guaranteed to be able to read any legal
text file a line at a time.
-
read_words.c uses scanf() to read
words, one at a time, from the user or a redirected file.
We assume no word is more than 80 characters long. We simply
print each word we encounter.
-
type.c uses getchar() to read a character,
capital() to capitalize it, and putchar() to print it back out;
type CTRL-D to stop the program. How can we use this program to
capitalize an entire file?
Last revised on 15 July 2012.