An application can use the OS/2 file system functions-DosOpen, DosRead, DosWrite, and DosClose-with the standard (predefined) devices. The application simply specifies the name of the device in the call to DosOpen, then uses the returned handle to read from and write to the device. When the application has finished using the device, the application should close the device handle by using DosClose.

The following code fragment shows how an application can open the COM1 device (serial port 1) and write the contents of a disk file to the communications port:

    #define INCL_DOSFILEMGR    /* File System values */
    #define INCL_DOSDEVIOCTL   /* DosDevIOCtl values */
    #include <os2.h>

    BYTE abBuf[512];
    HFILE hfCom, hfFile;
    ULONG ulAction, cbRead, cbWritten;

    DosOpen("COM1",
            &hfCom,
            &ulAction,
            0,
            FILE_NORMAL,
            FILE_OPEN,
            OPEN_ACCESS_READWRITE |
            OPEN_SHARE_DENYNONE,
            (PEAOP2) NULL);

    DosOpen("testfile",
            &hfFile,
            &ulAction,
            0,
            FILE_NORMAL,
            FILE_OPEN,
            OPEN_ACCESS_READONLY |
            OPEN_SHARE_DENYWRITE,
            (PEAOP2) NULL);

    do {
        DosRead(hfFile,
                abBuf,
                sizeof(abBuf),
                &cbRead);

        DosWrite(hfCom,
                 abBuf,
                 cbRead,
                 &cbWritten);

    } while(cbRead);

    DosClose(hfCom);

    DosClose(hfFile);

Note: In this example code fragment and the ones that follow, error checking was left out to conserve space. Applications should always check the return code that the functions return. Control Program functions return an APIRET value. A return code of 0 indicates success. If a non-zero value is returned, an error occurred.