Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Reading one char from standard Input without waiting for CR
« 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 Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show all replies
    • http://www.iki.fi/sintonen/
Re: Reading one char from standard Input without waiting for CR
« Reply #1 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).