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.
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.
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.
#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.