KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q66700: Redirecting Standard Input Does Not Affect getch() Under OS/2

Article: Q66700
Product(s): See article
Version(s): 6.00 6.00a | 6.00 6.00a
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 11-NOV-1990

Redirecting STDIN (standard input) in the OS/2 environment does not
change the behavior of the getch() function; it will continue to read
from the keyboard. This is different from the DOS environment where
redirecting STDIN also causes getch() to be redirected.

The getchar() function could be used instead because it reads from
standard input and will conform with redirection under OS/2. However,
if standard input is not redirected, you will have to press ENTER
after each character typed in. This can be nuisance when the program
does not know if input will be redirected or not.

A simple workaround is to use the FAPI function DosQHandType to
determine if standard input has been redirected or not. The program
can then call either getch() or getchar(), accordingly. The following
sample code illustrates the workaround:

Sample Code
-----------

#define INCL_VIO
#include<os2.h>
#include<conio.h>

void main(void)
{
   USHORT fsType, usDeviceAttr;

   DosQHandType(0, &fsType, &usDeviceAttr);

   if(LOBYTE(fsType) == HANDTYPE_DEVICE)
   {
      /* Standard input is not redirected. */
      /* Use getch() for input.            */
   }
   else if(LOBYTE(fsType) == HANDTYPE_FILE)
   {
      /* Standard input is redirected to a file. */
      /* Use getchar() for input.                */
   }
}

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.