HP3000-L Archives

January 2001, Week 3

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Bruce Toback <[log in to unmask]>
Reply To:
Bruce Toback <[log in to unmask]>
Date:
Wed, 17 Jan 2001 19:22:21 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (64 lines)
John Dass writes:

>I have started doing some programming on the MPE using
>C language and found that I cannot write a program in C
>to capture a single keypress from the user.

Someone else has already mentioned setbuf(), but...

>I emailed HP3K Anwerline and they suggested that I
>use the FREAD to the terminal to accomplish this. But
>being a novice programmer myself, I am unable to
>do this although I have tried reading many PDFs on
>this FREAD and FOPEN. I dunno how to FOPEN
>the STDIN and then FREAD a character and then
>pass control to the program.

Use READ() instead of FREAD. READ() reads $STDIN, which is usually the
same thing as stdin.

>The program I have is as follows (where I have
>to press a ENTER after pressing Y) :

There's a bug here, though:
>
>main()
>{
>   char c[1];

This allocates a one-byte buffer, but...
>
>   printf("Do you want to continue (Y/N) ?");
>   c = getchar();

c is an array, which means it's a pointer. Also, getchar() returns an
int. You want

    c[0] = (char) getchar();

if you're willing to ignore EOF. And you may as well get rid of the array
and just use a single char.

>   if strcmp(c,"Y")

strcmp() expects a null-terminated string, not a single character. If
you're lucky, this will work just because the byte following the array
happens to be zero. If not, it won't. Use

   if (c[0] == 'Y' || c[0] == 'y')

instead (to be friendly about case, too).

-- Bruce


--------------------------------------------------------------------------
Bruce Toback    Tel: (602) 996-8601| My candle burns at both ends;
OPT, Inc.            (800) 858-4507| It will not last the night;
11801 N. Tatum Blvd. Ste. 142      | But ah, my foes, and oh, my friends -
Phoenix AZ 85028                   | It gives a lovely light.
btoback AT optc.com                |     -- Edna St. Vincent Millay
Mail sent to [log in to unmask] will be inspected for a
fee of US$250. Mailing to said address constitutes agreement to
pay, including collection costs.

ATOM RSS1 RSS2