io_control |
Use any I/O class that takes an os_ioctl_t
parameter to control I/O characteristics at runtime. The control values for the
I/O device can be specified in several different ways.
To use the POSIX interface, you must construct the appropriate I/O control value by a bitwise-OR of one or more flags. The actual number of POSIX flags available can vary, depending on the operating system.
The following table lists some of the POSIX values available on most platforms.
Although POSIX flags are often
used in legacy applications, the definitions of these flags are not always
understood. The os_io_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 I/O
control constants predefined by the os_io_control
class, along with the POSIX equivalents.
Another approach to specifying a
control value for an I/O device applies to situations that require I/O control
values to be determined at runtime. In this technique, an os_io_control
object is instantiated, and its method interface is used to construct the I/O
control value dynamically.
The following example shows how to
use both the os_io_control object and POSIX flags
with the os_file class.
#include <iostream>
#include <ospace/file.h>
void
main()
{
os_file_toolkit initialize;
// Use POSIX flags to open a file for write-only
os_file file1
(
"ioctrl1.txt",
os_open_control::create_always,
O_WRONLY // POSIX interface
);
file1.write( "part 1 ", 7 );
file1.close();
// Use the constant interface to open a file for read-only.
os_file file2
(
"ioctrl1.txt",
os_open_control::open_existing,
os_io_control::read_only_access
);
char buffer[ 100 ];
int bytes = file2.read( buffer, sizeof( buffer ) );
cout << "Read " << bytes << " bytes: " << buffer << endl;
file2.close();
// Use the os_io_control class to open a file for read/write.
os_io_control io_control;
io_control.set_read_write();
io_control.append( true );
os_file file3
(
"ioctrl1.txt",
os_open_control::open_existing,
io_control
);
file3.write( "part 2\n", 8 );
file3.rewind();
bytes = file3.read( buffer, sizeof( buffer ) );
cout << "Read " << bytes << " bytes: " << buffer << endl;
file3.close();
}
Read 7 bytes: part 1
Read 15 bytes: part 1 part 2
Once an I/O object is
constructed, you can change its I/O control settings using the object's method
io_control() . Note that the O_RDONLY
, O_WRONLY , and O_RDWR
flags are only recognized when an I/O object is constructed.
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.