DAY32
Wednesday, 25 July 2012
Today we might talk about reading and writing files of text:
-
Using sprintf() to "write" information into a string.
-
Using fgetc() to read one character at a time from a text file
and fputc() to write out that character to another file;
-
Using fscanf() to read words one at a time from a text file,
and fprintf() to write those words to another file.
-
Using fgets() to read lines (or parts of lines) at a time
and fprintf() to write those lines to another file.
Programs and functions we might talk about:
-
brett.c shows how the sprintf() command
can "print" onto a string. We want to do this so that we can
create a sequence of file names in a loop as we set up an animation.
-
capital.c capitalizes a single character.
-
capital_string.c capitalizes all the
characters in a string.
-
ftype.c uses fgetc () to read a character
from "input.txt", capital() to capitalize it, and fputc() to print
it back out in a new file called "output.txt";
-
get_lines.c, a program which reads
the Gettysburg address one line at a time using fgets(). It capitalizes
each line, and then writes the line to a new file using fprintf();
-
gettysburg.txt, a text file containing
Abraham Lincoln's Gettysburg address.
-
gettysburg_cap.txt, the capitalized
version of the Gettysburg address.
-
input.txt an example input file for ftype.c.
-
output.txt the output file created by
ftype.c.
-
scan_words.c uses fscanf() on the
file gettysburg.txt to read one word at a time. Each word is
printed out, and numbered.
-
stars.png a PNG version of stars.ppm
-
stars.ppm an ASCII Portable Pixel Map (PPM)
color graphics file. It starts with "P3", then the number of columns and rows,
then the maximum color index (255), and then the R, G and B values for each pixel.
Last revised on 23 July 2012.