The standard input, standard output, and standard error files are usually the keyboard and screen, but not always. For example, if you redirect standard output by using the greater-than (>) redirection symbol on the application's command line, all data written to the standard output file goes to the given file.

The following command line redirects standard output to the file SAMPLE.TXT and redirects error messages to the file SAMPLE.ERR:

    type startup.cmd >sample.txt 2>sample.err
When a standard file is redirected, its handle is still available but corresponds to the given disk file instead of to the keyboard or screen. You can still use DosRead and DosWrite to read from and write to the files.

You can use DosDupHandle to redirect a standard file from inside your application. If you duplicate the standard input file handle, your application reads from the specified file rather than from the keyboard. Duplicating the standard output file handle causes output to be directed to a file or device instead of to the standard output device.

The following code fragment shows how to use the standard input handle to read from a file:

    #define INCL_DOSFILEMGR   /* File system values     */
    #include <os2.h>

    #define HF_STDIN  0       /* Standard input handle  */
    #define HF_STDOUT 1       /* Standard output handle */
    #define BUF_SIZE 80

    BYTE    abBuffer[BUF_SIZE];
    HFILE   hf,
            hfNew;
    ULONG   ulRead,
            ulWritten,
            ulAction;
    APIRET  ulrc;

    ulrc = DosOpen("SAMPLE.C",
                   &hf,
                   &ulAction,
                   0,
                   FILE_NORMAL,
                   FILE_OPEN,
                   OPEN_ACCESS_READONLY |
                   OPEN_SHARE_DENYNONE,
                   (PEAOP2) NULL);

    if (!ulrc) {

        hfNew = 0;      /* Duplicates standard input */

        DosDupHandle(hf,
                     &hfNew);

        DosRead(HF_STDIN,
                abBuffer,
                sizeof(abBuffer),
                &ulRead);

        DosWrite(HF_STDOUT,
                 abBuffer,
                 ulRead,
                 &ulWritten);
    }


[Back] [Next]