DAY31
Monday, 23 July 2012
Today we might talk about:
-
How to read a "word" (such as a filename) that is typed in by the user.
-
How to open a file, that is, make it available for reading or
writing, using fopen() (example: hello_file.c).
-
How to read numeric information from a file using fscanf();
-
How to write numeric information to a file using fprintf();
-
How to close a file, that is, to break the connection between
your program and the file, using fclose();
-
We will look at how the use_bw.c program sets up some data,
and then calls bw.c to write that data to a file, using a
special graphics format.
-
We will look at how the read1_write2 program reads an input file,
containing a table of numbers, and then writes versions of that
table to two new files, after changing a few values in the table.
Programs and functions we might talk about:
-
bw.c,
a function which creates a new file called "picture.pbm",
uses fprintf() to write information
to it, and then closes it. The information is a 2D array of 0's and 1's.
The function writes this array, and a little bit of extra information,
so that the file that is created is a Portable Bit Map graphics file
which can be displayed by many graphics programs. One such program
on our systems is called eog.
-
hello_file.c,
a program which writes information to a file and to the screen.
The file is created by using fopen(), fprintf() and fclose().
But note that the fprintf() command can also be used to print to
the screen, if we specify the file identifier as "stdout".
-
input.txt,
the file read by "read1_write2".
-
output1.txt,
the first file created by "read1_write2".
-
output2.txt,
the second file created by "read1_write2".
-
picture.pbm,
an example of the PBM file created by bw.c.
-
picture.png,
a PNG version of the PBM file.
-
read1_write2.c,
a program which reads an array from a file, and writes a copy
of the array to two new files.
-
use_bw.c,
a program which sets up an array of data, and calls bw.c
to write that data to a file in a special graphics format.
Last revised on 18 July 2012.