twitter
    Find out what I'm doing, Follow Me :)

Another Strange I/O Method

Load the file named singleio.c and display it on your monitor for another method of character I/O. Once again, we start with the standard I/O header file, we define a variable named "c", and we print a welcoming message. Like the last program, we are in a loop that will continue to execute until we type a capital X, but the action is a little different here.

# include "/sys/stdio.h" /* standard header for input/output */


main( )
{
char c;
printf("Enter any characters, terminate program with X\n");
do {
c = getch(); /* Get a character */
putchar(c); /* Display the hit key */
} while (c != ’X’);
printf("\nEnd of program.\n");
}



The "getch()" is a new function that is a "get character" function. It differs from "getchar()" in 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.