INF: Redirecting stdin Does Not Affect getch Under OS/2

ID Number: Q66700

6.00 6.00a

OS/2

Summary:

In Microsoft C versions 6.0 and 6.0a, 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

behavior differs from the MS-DOS environment where redirecting stdin

also causes getch to be redirected.

More Information:

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 (Family API) 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. */

}

}

Additional reference words: 6.00 6.00a