home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
c
/
c_tutor
/
CHAP14_TXT
< prev
next >
Wrap
Text File
|
1987-11-21
|
14KB
|
323 lines
Chapter 14 - Example Programs
WHY THIS CHAPTER?
Although every program in this tutorial has been a
complete program, each one has also been a very small
program intended to teach you some principle of programming
in C. It would do you a disservice to leave you at that
point without introducing you to a few larger programs to
illustrate how to put together the constructs you have
learned to create a major program. This chapter contains
four programs of increasing complexity, each designed to
take you into a higher plateau of programming, and each
designed to be useful to you in some way.
DOSEX will illustrate how to make DOS system calls and
will teach you, through self-study, how the system responds
to the keyboard. WHATNEXT reads commands input on the
command line and will aid you in setting up a variable batch
file, one that requests an operator input and responds to
the input by branching to a different part of the batch
file.
LIST is the source code for the program you used to
print out the C source files when you began studying C with
the aid of this tutorial. Finally we come to VC, the Visual
Calculator, which you should find to be a useful program
even if you don't study its source code. VC uses most of
the programming techniques we have studied in this course
and a few that we never even mentioned such as separately
compiled subroutines.
We will take a look at the example programs one at a
time but without a complete explanation of any of them
because you have been studying C for some time now and
should be able to read and understand most of these programs
on your own.
DOSEX.C - The DOS Example Program
The copy of DOS that you received with your IBM-PC or
compatible has about 80 internal DOS calls that you can use
as a programmer to control your peripheral devices and read
information or status from them. Some of the earlier IBM
DOS manuals, DOS 2.0 and earlier, have these calls listed in
the back of the manual along with how to use them. Most of
the manuals supplied with compatible computers make no
mention of these calls even though they are extremely
useful. These calls can be accessed from nearly any
programming language but they do require some initial study
to learn how to use them. This program is intended to aid
you in this study.
Page 99
Chapter 14 - Example Programs
Display the program on your monitor or print it out for
reference. It is merely a loop watching for a keyboard
input or a change in the time. If either happens, it reacts
accordingly. In line 32, the function "kbhit()" returns a
value of 1 if a key has been hit but not yet read from the
input buffer by the program.
Look at the function named "get_time" for an example of
a DOS call. An interrupt 21(hex) is called after setting
the AH register to 2C(hex) = 44(decimal). The time is
returned in the CH, CL, and DH registers. Refer to the DOS
call definitions in your copy of DOS. If the definitions
are not included there, Peter Nortons book, "Programmers
Guide to the IBM PC" is recommended as a good reference
manual for these calls and many other programming
techniques. Your compiler may have a built in function to
do this. If you read your documentation, you will probably
find many useful functions available with your compiler that
are included as a convenience for you by your compiler
writer.
Another useful function is the "pos_cursor()" function
that positions the cursor anywhere on the monitor that you
desire by using a DOS interrupt. In this case, the
interrupt used is 10(hex) which is the general monitor
interrupt. This particular service is number 2 of about 10
different monitor services available. This function is
included here as another example to you.
The next function, service number 6 of interrupt
10(hex) is the window scroll service. It should be self
explanatory.
In this program, the cursor is positioned and some data
is output to the monitor, then the cursor is "hidden" by
moving it to line 26 which is not displayed. After you
compile and run the program, you will notice that the cursor
is not visible on the monitor. This is possible in any
program, but be sure to put the cursor in view before
returning to DOS because DOS does not like to have a
"hidden" cursor and may do some strange things.
Some time spent studying this program will be valuable
to you as it will reveal how the keyboard data is input to
the computer. Especially of importance is how the special
keys such as function keys, arrows, etc. are handled. Also
note that this program uses full prototype checking and is a
good example of how to use it. Since it also uses the
"modern" method of function definitions, it is a good
example of that also.
Page 100
Chapter 14 - Example Programs
WHATNEXT.C - The Batch File Interrogator
This is an example of how to read the data on the
command line following the function call. Notice that there
are two variables listed within the parentheses following
the main() call. The first variable is a count of words in
the entire command line including the command itself and the
second variable is a pointer to an array of pointers
defining the actual words on the command line.
First the question on the command line, made up of some
number of words, is displayed on the monitor and the program
waits for the operator to hit a key. If the key hit is one
of those in the last "word" of the group of words on the
command line, the number of the character within the group
is returned to the program where it can be tested with the
"errorlevel" command in the batch file. You could use this
technique to create a variable AUTOEXEC.BAT file or any
other batch file can use this for a many way branch.
Compile and run this file with TEST.BAT for an example of
how it works in practice. You may find this technique
useful in one of your batch files and you will almost
certainly need to read in the command line parameters
someday.
An interesting alternative would be for you to write a
program named "WOULD.C" that would return a 1 if a "Y" or
"y" were typed and a zero if any other key were hit. Then
your batch file could have a line such as;
WOULD YOU LIKE TO USE THE ALTERNATIVE METHOD (Y/N)
Dos would use "WOULD" as the program name, ignore the
rest of the statement except for displaying it on the
screen. You would then respond to the question on the
monitor with a single keyhit. Your batch file would then