[ Pobierz całość w formacie PDF ]
that it does not get tied up in DOS. It reads the character in without echo, and puts it directly
into the program where it is operated on immediately. This function then reads a character
immediately displays it on the screen, and continues the operation until a capital X is typed.
You wil recognise that using this "getch()" function accidently in the #include "stdio.h" caused
problems in the previous program.
When you compile and run this program, you will find that there is no repeat of the lines when
you hit a carriage return, and when you hit the capital X, the program terminates immediately.
No carriage return is needed to get it to accept the line with the X in it. We do have another
problem here, there is no linefeed with the carriage return.
9-3 C Tutorial Standard Input/Output
9.7 Now We Need A Line Feed
It is not apparent to you in most application programs but when you hit the enter key, the program
supplies a linefeed to go with the carriage return. You need to return to the left side of the
monitor and you also need to drop down a line. The linefeed is not automatic. We need to
improve our program to do this also. If you will load and display the program namedbet-
terin.c,you will find a change to incorporate this feature.
# include "stdio.h"
# define CR 13 /* this defines CR to be 13 */
# define LF 10 /* this defines LF to be 10 */
main( )
{
char c;
printf("input any characters,hit X to stop.\n");
do {
c = getch(); /* get a character */
putchar(c); /* display the hit key */
if (c == CR) putchar(LF); /* if it is a carriage return
put out a linefeed too */
} while (c != X );
printf("\nEnd of program.\n");
}
In betterin.c, we have two additional statements at the beginning that will define the
character codes for the linefeed (LF), and the carriage return (CR). If you look at any ASCII
table you will find that the codes 10 and 13 are exactly as defined here. In the main program,
after outputting the character, we also output a linefeed which is the LF. We could have just as
well have left out the two #define statements and used "if (c == 13) putchar(10);" but it would
not be very descriptive of what we are doing here. The method used in the program represents
better programming practice.
Compile and run betterin.c to see if it does what we have said it should do. It should
display exactly what you type in, including a linefeed with each carriage return, and should stop
immediately when you type a capital X.
If you are using a nonstandard compiler, it may not find a "CR" because your system returns a
"LF" character to indicate end-of-line. It will be up to you to determine what method your
compiler uses. The quickest way is to add a "printf" statement that prints the input character in
decimal format.
9.8 Which Method Is Best?
We have examined two methods of reading characters into a C program, and are faced with a
choice of which one we should use. It really depends on the application because each method
has advantages and disadvantages. Lets take a look at each.
When using the first method, 1616/OS is actually doing all of the work for us, by storing the
characters in an input buffer and signalling us when a full line has been entered. We could write
a program that, for example, did a lot of calculations, then went to get some input. While we
were doing the calculations, 1616/OS would be accumulating a line of characters for us, and
they would be there when we were ready for them. However, we could not read in single
keystrokes because 1616/OS would not report a buffer of characters to us until it recognized a
carriage return.
Standard Input/Output C Tutorial 9-4
The second method, used inbetterin.c, allows us to get a single character, and act on it
immediately. We do not have to wait until 1616/OS decides we can have a line of characters.
We cannot do anything else while we are waiting for a character because we are waiting for the
input keystroke and tying up the entire machine. This method is useful for highly interactive
types of program interfaces. It is up to you as the programmer to decide which is best for your
needs.
I should mention at this point that there is also an "ungetch" function that works with the "getch"
[ Pobierz całość w formacie PDF ]