DosOpen is used to create files, which are then read from or written to. To create a file, specify FILE_CREATE as the sixth argument in the call to the function. DosOpen then creates the file, if it does not already exist. If the file already exists, the function returns the error value FILE_EXISTED.

The following code fragment shows how to use DosOpen to create the file NEWFILE.TXT:

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

    HFILE   hf;
    ULONG   ulAction;
    APIRET  ulrc;

    ulrc = DosOpen("NEWFILE.TXT",       /* Name of file to create and open */
                   &hf,                 /* Address of file handle          */
                   &ulAction,           /* Action taken                    */
                   0,                   /* Size of new file                */
                   FILE_NORMAL,         /* File attributes                 */
                   FILE_CREATE,         /* Creates the file                */
                   OPEN_ACCESS_WRITEONLY |
                   OPEN_SHARE_DENYNONE,
                   (PEAOP2) NULL);      /* No extended attributes          */

In this example, DosOpen creates the file and opens it for writing only. Note that the sharing method chosen permits other processes to open the file for any access. The new file is empty (contains no data).

When you use DosOpen to create (or replace) a file, you must specify the attributes the new file is to have. In the preceding example, this value is FILE_NORMAL, so the file is created as a normal file. Other possible file attributes include read-only and hidden, which correspond to FILE_READONLY and FILE_HIDDEN, respectively. The possible file attributes are: