You can close a file by using DosClose. Since each application has a limited number of file handles that can be open at one time, it is a good practice to close a file after using it.

To do so, supply the file handle in DosClose, as shown in the following code fragment:

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

    #define BUF_SIZE 80

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

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

    if (!ulrc) {
        DosRead(hf,
                abBuffer,
                sizeof(abBuffer),
                &ulRead);

        DosClose(hf);
    }

If you open a file for writing, DosClose directs the system to flush the file buffer-that is, to write any existing data in OS/2's intermediate file buffer to the disk or device. The system keeps these intermediate file buffers to make file input and output more efficient. For example, it saves data from previous calls to DosWrite until a certain number of bytes are in the buffer then writes the contents of the buffer to the disk.

DosClose also closes the handle to the file (or pipe, or device). If one or more additional handles to a file have been created with DosDupHandle , the internal buffers for the file are not written to disk, and its directory entry is not updated, until DosClose has been called for all the handles.


[Back] [Next]