www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/18/05:30:00

Date: Tue, 18 Feb 1997 12:16:14 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Stephen Lyda <steve AT xactinc DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: IO Buffering
In-Reply-To: <33086AA3.51E6@xactinc.com>
Message-ID: <Pine.SUN.3.91.970218121516.20000G-100000@is>
MIME-Version: 1.0

On Mon, 17 Feb 1997, Stephen Lyda wrote:

> Basically, I am trying to cause getchar() to work correctly, returning
> after a single key is pressed, and not waiting until the buffer is 
> full or a CR is encountered.

You need to switch stdin to raw mode as well as make it unbuffered.
Only raw (as opposed to cooked) input from the console device is done
by single characters.  In cooked mode DOS itself won't return to the
caller until it sees a CR; there's nothing DJGPP can do about this DOS
feature.

The simplest way to switch stdin to raw mode is to switch it to binary
(in DJGPP, this also calls the appropriate IOCTL subfunction to put
console into raw mode).  Here's an example program that works for me:

	#include <stdio.h>
	#include <io.h>
	#include <fcntl.h>
	#include <conio.h>

	int main (void)
	{
	  int c;

	  setvbuf (stdin, 0, _IONBF, 0);
	  setmode (fileno (stdin), O_BINARY);
	  __djgpp_set_ctrl_c (1);

	  while ((c = getchar ()) != ' ')
	    putch (c);

	  return 0;
	}

This will read characters one by one and echo them until you press
SPACE.  The call to `__djgpp_set_ctrl_c' (look it up in the libc
reference docs) is to make the program interruptible when you press
Ctrl-C; this might or might not be appropriate for the application you
have in mind.  For example, if you need to get Ctrl-C as a normal
character, you should NOT call that function (use Ctrl-Break instead
to cause a SIGINT).

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019