home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
c
/
c_tutor
/
CHAP10_TXT
< prev
next >
Wrap
Text File
|
1987-11-21
|
18KB
|
455 lines
Chapter 10 - File Input/Output
OUTPUT TO A FILE
Load and display the file named FORMOUT.C for your
first example of writing data to a file. We begin as before
with the "include" statement for "stdio.h", then define some
variables for use in the example including a rather strange
looking new type.
The type "FILE" is used for a file variable and is
defined in the "stdio.h" file. It is used to define a file
pointer for use in file operations. The definition of C
contains the requirement for a pointer to a "FILE", and as
usual, the name can be any valid variable name.
OPENING A FILE
Before we can write to a file, we must open it. What
this really means is that we must tell the system that we
want to write to a file and what the filename is. We do
this with the "fopen" function illustrated in the first line
of the program. The file pointer, "fp" in our case, points
to the file and two arguments are required in the
parentheses, the filename first, followed by the file type.
The filename is any valid DOS filename, and can be expressed
in upper or lower case letters, or even mixed if you so
desire. It is enclosed in double quotes. For this example
we have chosen the name TENLINES.TXT. This file should not
exist on your disk at this time. If you have a file with
this name, you should change its name or move it because
when we execute this program, its contents will be erased.
If you don't have a file by this name, that is good because
we will create one and put some data into it.
READING ("r")
The second parameter is the file attribute and can be
any of three letters, "r", "w", or "a", and must be lower
case. There are actually additional attributes available in
C to allow more flexible I/O. When an "r" is used, the file
is opened for reading, a "w" is used to indicate a file to
be used for writing, and an "a" indicates that you desire to
append additional data to the data already in an existing
file. Opening a file for reading requires that the file
already exist. If it does not exist, the file pointer will
be set to NULL and can be checked by the program.
WRITING ("w")
When a file is opened for writing, it will be created
if it does not already exist and it will be reset if it does
resulting in deletion of any data already there.
Page 70
Chapter 10 - File Input/Output
APPENDING ("a")
When a file is opened for appending, it will be created
if it does not already exist and it will be initially empty.
If it does exist, the data input point will be the end of
the present data so that any new data will be added to any
data that already exists in the file.
OUTPUTTING TO THE FILE
The job of actually outputting to the file is nearly
identical to the outputting we have already done to the
standard output device. The only real differences are the
new function names and the addition of the file pointer as
one of the function arguments. In the example program,
"fprintf" replaces our familiar "printf" function name, and
the file pointer defined earlier is the first argument
within the parentheses. The remainder of the statement
looks like, and in fact is identical to, the "printf"
statement.
CLOSING A FILE
To close a file, you simply use the function "fclose"
with the file pointer in the parentheses. Actually, in this
simple program, it is not necessary to close the file
because the system will close all open files before
returning to DOS. It would be good programming practice for
you to get in the habit of closing all files in spite of the
fact that they will be closed automatically, because that
would act as a reminder to you of what files are open at the
end of each program.
You can open a file for writing, close it, and reopen
it for reading, then close it, and open it again for
appending, etc. Each time you open it, you could use the
same file pointer, or you could use a different one. The
file pointer is simply a tool that you use to point to a
file and you decide what file it will point to.
Compile and run this program. When you run it, you
will not get any output to the monitor because it doesn't
generate any. After running it, look at your directory for
a file named TENLINES.TXT and "type" it. That is where your
output will be. Compare the output with that specified in
the program. It should agree.
Do not erase the file named TENLINES.TXT yet. We will
use it in some of the other examples in this chapter.
Page 71
Chapter 10 - File Input/Output
OUTPUTTING A SINGLE CHARACTER AT A TIME
Load the next example file, CHAROUT.C, and display it
on your monitor. This program will illustrate how to output
a single character at a time.
The program begins with the "include" statement, then
defines some variables including a file pointer. We have
called the file pointer "point" this time, but we could have
used any other valid variable name. We then define a string
of characters to use in the output function using a "strcpy"
function. We are ready to open the file for appending and
we do so in the "fopen" function, except this time we use
the lower cases for the filename. This is done simply to
illustrate that DOS doesn't care about the case of the
filename. Notice that the file will be opened for appending
so we will add to the lines inserted during the last
program.
The program is actually two nested "for" loops. The
outer loop is simply a count to ten so that we will go
through the inner loop ten times. The inner loop calls the
function "putc" repeatedly until a character in "others" is
detected to be a zero.
THE "putc" FUNCTION
The part of the program we are interested in is the
"putc" function. It outputs one character at a time, the
character being the first argument in the parentheses and
the file pointer being the second and last argument. Why
the designer of C made the pointer first in the "fprintf"
function, and last in the "putc" function is a good question
for which there may be no answer. It seems like this would
have been a good place to have used some consistency.
When the textline "others" is exhausted, a newline is
needed because a newline was not included in the definition
above. A single "putc" is then executed which outputs the
"\n" character to return the carriage and do a linefeed.
When the outer loop has been executed ten times, the
program closes the file and terminates. Compile and run