Welcome, Guest. Please login or register.

Author Topic: Reading one char from standard Input without waiting for CR  (Read 1669 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline kwoolridgeTopic starter

  • Newbie
  • *
  • Join Date: Sep 2007
  • Posts: 8
    • Show only replies by kwoolridge
Reading one char from standard Input without waiting for CR
« on: September 14, 2007, 09:38:16 PM »
Does anyone know whether any of the AmigaOS Libraries contain a routine which will read one character from the standard input (not a raw window, but the standard shell window) without waiting for a carriage return (i.e. pressing the Enter key)?

Other OS's (VMS, MPX, even Windows) have such routines.  Any help is appreciated.  Thanks.
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Reading one char from standard Input without waiting for CR
« Reply #1 on: September 14, 2007, 10:08:34 PM »
Such routines are non-standard and thus it's not readily available without some hacking. It's possible however, by using raw mode.

Code: [Select]
#include <proto/dos.h>

LONG readchar(BPTR fh);

int main(void)
{
  Printf(&quot;0x%02lx\n&quot;, readchar(Input()));

  return 0;
}

LONG readchar(BPTR fh)
{
  LONG ret = -1;

  if (IsInteractive(fh))
  {
    if (SetMode(fh, 1)) /* Attempt to go to raw mode */
    {
      UBYTE buf[4];
      LONG actual;

      do
      {
        actual = Read(fh, buf, sizeof(buf));
        /* Read until single char or error */
      } while (actual > 1);

      SetMode(fh, 0); /* Go back to con mode */

      if (actual == 1)
      {
        ret = buf[0];
      }
    }
  }
  else
  {
    ret = FGetC(fh);
  }

  return ret;
}
 

Offline Stedy

  • Sr. Member
  • ****
  • Join Date: Jul 2002
  • Posts: 259
    • Show only replies by Stedy
    • http://www.ianstedman.co.uk
Re: Reading one char from standard Input without waiting for CR
« Reply #2 on: September 14, 2007, 10:14:38 PM »
Hi,

If you are using C, the setbuf() or better, the setvbuf() function will do what you want.

the command
setvbuf(fp,NULL,_IONBF,0);

Will turn off buffering in a console window, you can then use getchar() or similar to read the character thus:

Example program (tested with Dice C, copied from the examples)

#include
#define BUFFER_SIZE 128
main()
{
   int c;
   char buf[256];
 
   setvbuf(stdin,NULL, _IONBUF,0);
   printf("Type a character: ");
   fflush(stdout);
   c=getchar();
   printf("c=%d\n");

return(0);
}

Ian
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Reading one char from standard Input without waiting for CR
« Reply #3 on: September 14, 2007, 10:19:00 PM »
@Stedy

_IONBF, missing argument for printf. Surely Dice C doesn't have such "quality" examples?

And it still doesn't work for me (SAS/C, gcc libnix, gcc ixemul).
 

Offline kwoolridgeTopic starter

  • Newbie
  • *
  • Join Date: Sep 2007
  • Posts: 8
    • Show only replies by kwoolridge
Re: Reading one char from standard Input without waiting for CR
« Reply #4 on: September 21, 2007, 01:13:27 PM »
Thank you for your responses!