Amiga.org
Amiga computer related discussion => Amiga Software Issues and Discussion => Topic started by: kwoolridge 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.
-
Such routines are non-standard and thus it's not readily available without some hacking. It's possible however, by using raw mode.
#include <proto/dos.h>
LONG readchar(BPTR fh);
int main(void)
{
Printf("0x%02lx\n", 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;
}
-
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
-
@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).
-
Thank you for your responses!