CHAPTER 5
The Control-C key combination not only causes the
Cconrs() function to return, but also terminates the entire
program. This feature makes Cconrs() extremely dangerous,
and suitable only for quick and dirty programs that you write
for your own use. Any program written for use by others
should implement its own input routine in a manner that
doesn't allow the user to exit the program easily by mistake.
Program 5-1 demonstrates some of the character I/O
functions discussed above.
Program 5-1. GCHARDEV.C
/**********************************************/
/* */
/* GCHARDEV.C */
/* */
/* Demonstrates some of the GEMDOS */
/* character device functions. */
/* */
/**********************************************/
flinclude <osbind.h> /* For GEMDOS macro definitions */
^define MAXLEN 80 /* maximum input line length */
main()
{
char buf[MAXLEN+3], ch;
int len=0;
/* first input a line a character at a time, using Cconin */
Cconws("Enter a line of text, and hit Return:\n\n\r");
do /* keep getting characters */
if ( (ch=Cconin()) != 8 ) /* if this isn't a backspace, */
buf[len++]=ch; /* add the character to the buffer */
else
{ /* if it is a backspace, */
Cconws(" \010"); /* rub out last character...*/
if (len>0 ) /* and if there are any chars to delete */
len— ; /* delete one */
} while( (ch!=13) && (len<MAXLEN) ); /* do til CR or end of line */
buf[len]=0; /* terminate line with ASCII */
Cconws("\n\nYour line of text was:\n\r");
Cconws(buf);
printf("\nAnd it was %d characters long\n\n\n",len);
/* Now, input the whole line at a time */
Cconws("Enter another line of text, and hit Return:\n\n\r");
buf[0]=MAXLEN;
len =Cconrs(buf);
buf[len+2 ]=0;
Cconws("\n\nYour line of text was:\n\r");
Cconws(buf+2);
printf("\nAnd it was %d characters long\n",len);
/* if you're running this from the Desktop, you may want to
add a pause to give the user a chance to read the output */
Cconws("\n\n\rPress any key to end");
Cconin();
)
end of GCHARDEV.C *****/
94