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:
File Attribute
The file attribute affects how other processes access the file. For example, if the file is read-only, no process can open the file for writing. There is one exception-the process that creates the read-only file can write to it immediately after creating it. After closing the file, however, the process cannot reopen it for writing.
If you are creating a new file object (a new file or a replacement for an existing one), you must specify the size of the new file in bytes. For example, if you specify 256, the file size is 256 bytes. However, these 256 bytes are undefined. If the file being opened already exists, the file size parameter is ignored. It is up to the application to write valid data to the file. No matter what size you specify, the file pointer is set to point to the beginning of the file so a subsequent call to DosWrite starts writing data at the beginning of the file.
Extended attributes can be defined by an application when a file object is created. An application can define an extended attribute for a file when the file is created during a DosOpen call.
Applications can also control access to specific regions within a file by calling DosSetFileLocks.