home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
252_01
/
chap09.txt
< prev
next >
Wrap
Text File
|
1990-02-15
|
29KB
|
648 lines
Chapter 9
STANDARD INPUT/OUTPUT
THE STDIO.H HEADER FILE
____________________________________________________________
Load the file SIMPLEIO.C for our first look ==============
at a file with standard I/O. Standard I/O SIMPLEIO.C
refers to the most usual places where data is ==============
either read from, the keyboard, or written
to, the video monitor. Since they are used
so much, they are used as the default I/O devices and do not
need to be named in the Input/Output instructions. This will
make more sense when we actually start to use them so lets
look at the file in front of you.
The first thing you should take notice of is the second line
of the file, the line with #include "stdio.h". This is very
much like the #define we have already studied, except that
instead of a simple substitution, an entire file is read in
at this point. The system will find the file named STDIO.H
and read its entire contents in, replacing this statement.
Obviously then, the file named STDIO.H must contain valid C
source statements that can be compiled as part of a program.
This particular file is composed of several standard #defines
to define some of the standard I/O operations. The file is
called a header file and you will find several different
header files on the source disks that came with your C
compiler. Each of the header files has a specific purpose and
any or all of them can be included in any program.
Your C compiler uses the double quote marks to indicate that
the search for the include file will begin in the current
directory, and if it not found there, the search will continue
in the include directory as set up in the environment. It
also uses the "less than" and "greater than" signs to indicate
that the file search should begin in the directory specified
in the environment. Most of the programs in this tutorial
have the double quotes in the include statements. The next
program uses the "<" and ">" to illustrate the usage. Note
that this will result is a slightly faster (but probably
unnoticeable) compilation because the system will not bother
to search the current directory first.
INPUT/OUTPUT OPERATIONS IN C
____________________________________________________________
Actually the C programming language has no input or output
operations defined as part of the language, they must be user
defined. Since everybody does not want to reinvent his own
input and output operations, the compiler writers have done
Page 9-1
Chapter 9 - Standard Input/Output
a lot of this for us and supplied us with several input
functions and several output functions to aid in our program
development. The functions have become a standard, and you
will find the same functions available in nearly every
compiler. In fact, the industry standard of the C language
definition has become the book written by Kernigan and
Ritchie, and they have included these functions in their
definition. You will often, when reading literature about C,
find an author refer to K & R. This refers to the book, "The
C Programming Language", written by Kernigan and Ritchie. You
would be advised to purchase a copy for reference. As of this
writing, a second edition of this book is available and is the
preferred edition.
You should print out the file named STDIO.H and spend some
time studying it. There will be a lot that you will not
understand about it, but parts of it will look familiar. The
name STDIO.H is sort of cryptic for "standard input/output
header", because that is exactly what it is. It defines the
standard input and output functions in the form of #defines
and macros. Don't worry too much about the details of this
now. You can always return to this topic later for more study
if it interests you, but you will really have no need to
completely understand the STDIO.H file. You will have a
tremendous need to use it however, so these comments on its
use and purpose are necessary.
OTHER INCLUDE FILES
____________________________________________________________
When you begin writing larger programs and splitting them up
into separately compiled portions, you will have occasion to
use some statements common to each of the portions. It would
be to your advantage to make a separate file containing the
statements and use the #include to insert it into each of the
files. If you want to change any of the common statements,
you will only need to change one file and you will be assured
of having all of the common statements agree. This is getting
a little ahead of ourselves but you now have an idea how the
#include directive can be used.
BACK TO THE FILE NAMED SIMPLEIO.C
____________________________________________________________
Lets continue our tour of the file in question. The one
variable named c is defined and a message is printed out with
the familiar printf() function. We then find ourselves in a
continuous loop as long as the value of c is not equal to
capital X. If there is any question in your mind about the
loop control, you should review chapter 3 before continuing.
The two new functions within the loop are of paramount
interest in this program since they are the new functions.
Page 9-2
Chapter 9 - Standard Input/Output
These are functions to read a character from the keyboard and
display it on the monitor one character at a time.
The function getchar() reads a single character from the
standard input device, the keyboard being assumed because that
is the standard input device, and assigns it to the variable
named c. The next function putchar(), uses the standard
output device, the video monitor, and outputs the character
contained in the variable named c. The character is output
at the current cursor location and the cursor is advanced one
space for the next character. The system is therefore taking
care of a lot of the overhead for us. The loop continues
reading and displaying characters until we type a capital X
which terminates the loop.
Compile and run this program for a few surprises. When you
type on the keyboard, you will notice that what you type is
displayed faithfully on the screen, and when you hit the
return key, the entire line is repeated. We only told it to
output each character once but it seems to be saving the
characters up and redisplaying them. A short explanation is
in order.
DOS IS HELPING US OUT
____________________________________________________________
We need to understand a little bit about how DOS works to
understand what is happening here. When data is read from the
keyboard, under DOS control, the characters are stored in a
buffer until a carriage return is entered at which time the
entire string of characters is given to the program. When the
characters are being typed, however, the characters are
displayed one at a time on the monitor. This is called echo,
and happens in many of the applications you run.
With the above paragraph in mind, it should be clear that when
you are typing a line of data into SIMPLEIO, the characters
are being echoed by DOS, and when you return the carriage by
hitting return or enter, the characters are given to the
program. As each character is given to the program, it
displays it on the screen resulting in a repeat of the line
typed in. To better illustrate this, type a line with a
capital X somewhere in the middle of the line. You can type
as many characters as you like following the X and they will
all display because the characters are being read in under
DOS, echoed to the monitor, and placed in the DOS input
buffer. DOS doesn't think there is anything special about a
capital X. When the string is given to the program, however,
the characters are accepted by the program one at a time and
sent to the monitor one at a