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.
The second method, used in betterin.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" function. If you "getch" a character and find that you have gone one too far, you can "ungetch" it back to the input device. This simplifies some programs because you don’t know that you don’t want the character until you get it. You can only "ungetch" one character back to the input device, but that is sufficient to accomplish the task this function was designed for. It is difficult to demonstrate this function in a simple program so its use will be up to you to study when you need it.
The discussion so fact in this chapter, should be a good indication that, while the Cprogramming language is very flexible, it does put a lot of responsibility on you as the programmer to keep many details in mind.
The second method, used in betterin.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" function. If you "getch" a character and find that you have gone one too far, you can "ungetch" it back to the input device. This simplifies some programs because you don’t know that you don’t want the character until you get it. You can only "ungetch" one character back to the input device, but that is sufficient to accomplish the task this function was designed for. It is difficult to demonstrate this function in a simple program so its use will be up to you to study when you need it.
The discussion so fact in this chapter, should be a good indication that, while the Cprogramming language is very flexible, it does put a lot of responsibility on you as the programmer to keep many details in mind.