open_control


Use an I/O class with a constructor that takes an os_open_t parameter to either access an existing object in the system or create a new object. The os_open_t parameter specifies which open control mode the I/O class should use.

There are several techniques for specifying the open control mode. Each technique either uses the os_open_control class or the legacy POSIX interface.

To use the POSIX interface, the appropriate open control value must be constructed by a bitwise-OR of the following POSIX flags.
 
POSIX Flag Definition

O_CREAT

Creates the object, if it does not exist, and opens it. You cannot use O_CREAT with O_OPEN .

O_EXCL

If O_CREAT is specified and the object already exists, generates an event.

O_OPEN

Opens the object if it exists; otherwise, generates an event. You cannot use O_OPEN with O_CREAT .

O_TRUNC (files only)

If the file exists, truncates its length to zero.


Although POSIX flags are often used in legacy applications, the definitions of these flags are not always understood. The os_open_control class provides a set of predefined constants that can be used in place of the POSIX flags. These constants are portable across all platforms.

The following table lists all open control constants predefined by the os_open_control class, along with the POSIX equivalents.
 
Constant Definition POSIX Equivalent

create_always

Always creates and opens a new object. If the object already exists, truncates its contents.

O_CREAT|O_TRUNC

create_new

Only creates an object if it does not exist. If the object already exists, the operation fails.

O_CREAT|O_EXCL

open_always

Always opens the object. If the object does not exist, creates an empty object.

O_CREAT

open_existing

Only opens an existing object. If the object does not exist, the operation fails.

O_OPEN

truncate_existing

Only opens an existing object and truncates its contents. If the object does not exist, the operation fails.

O_OPEN|O_TRUNC

Another approach to specifying an open control mode applies to situations that require open control modes to be determined at runtime. In this technique, an os_open_control object is instantiated, and its method interface is used to construct the open control mode dynamically.

In the following example, the techniques described above are used to construct open control mode values used by the os_file class.

Example <ospace/io/examples/openctl1.cpp>
#include <iostream>
#include <ospace/file.h>

void
display( os_file& file )
  {
  file.rewind();
  char buffer[ 100 ];
  int bytes = file.read( buffer, sizeof( buffer ) );
  cout << "Read " << bytes << " bytes: " << buffer << endl;
  }

void
main()
  {
  os_file_toolkit initialize;

  try
    {
    // Use the legacy POSIX interface.
    os_file file1( "openctl1.txt", O_CREAT | O_TRUNC, O_RDWR );
    file1.write( "there\n", 7 );
    display( file1 );
    file1.close();

    // Use the predefined values in os_open_control.
    os_file file2
      (
      "openctl1.txt",
      os_open_control::open_existing,
      os_io_control::read_write_access
      );
    file2.write( "hi\n", 3 );
    display( file2 );
    file2.close();

    // Use the method interface to dynamically build the open mode.
    os_open_control open_control;
    open_control.create( true );
    open_control.exclusive( true );
    os_file file3( "openctl1.txt", open_control, O_RDWR );
    }
  catch ( os_file_toolkit_error& error )
    {
    cout << "Caught os_file_toolkit_error:" << endl;
    cout << "\t" << error.what() << endl;
    }
  }

Read 7 bytes: there

Read 7 bytes: hi
re

Caught os_file_toolkit_error:
        system_call_failure:
              ::open
                (
                openctl1.txt, // (char*) path.c_str()
                1282 // open | control
                ) [file.cpp:124]
		

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.