I need help

I need help

Post by Chri » Wed, 13 Jan 1993 17:42:29



I am just trying to do something simple.  I need a way to determine if
keys are present in the input buffer from the console.  I did read the
FAQ.  I tried ioctl and select, both to no avail.  This is largely due
to poor exampless manuals.
Please help me create a working function.  I am not a newbie when it comes
to C programming (or C++ for that matter!), however, most of my
experience lies with IBM PCs.

Basically, all I need is: if(kbhit()) ch=getchar();

I tried ioctl like this:
   int count;
   ioctl(stdin/*not sure what goes here*/,FIONREAD,&count);
But of course this doesnt work.

What follows is what I tried with select.

--------cut-----
#include <stdio.h>
#include <sys/time.h>

int     nfound, nfds, readfds, writefds, exceptfds;
struct timeval timeout;

/* How many seconds between queue scans to start off */
#define DEFAULT_PERIOD 300
void main()
{
   int z,ch;
   nfds = 1;
   writefds = 0;
   readfds = 1;
   exceptfds = 0;
   timeout.tv_sec = 0;/*DEFAULT_PERIOD*/;
   timeout.tv_usec = 0;

   /* this is a bitmap and the 1 specifies to look at stdin only */
   readfds = 1;

   /* the select call will wait for the specified period of time if
      no characters are available on stdin.  It will return as soon
      as any characters are available.  If the timeout struct was
      set to 0 it would return immediately even if nothing was available,
      but then it would return a 0.  */
puts("Running:");
for(ch=0;ch!=27;){
   nfound = select(nfds,&readfds,&writefds,&exceptfds,&timeout);
   printf("nfound: %d:4",nfound);
   if (nfound) {
      printf("read %c\n",ch=getchar());
      /* something was typed *{
      }        /* the stuff in this if..else statement is just test BS*/
   else {
      printf("%d10:\r",z++);
      /* nothing was typed */
      }

Quote:}
}

/*I realize the indenting is off...*/

--