The Str
class supports both C-style and C++-style I/O
functions.
If you choose to use C I/O, the char*
conversion
functions detailed in chapters 5 and
6 will give you the tools you need to interface
the C I/O library. The main function for C output is the const char*
casting function. The main functions for C input are
attach()
, detach()
and constructors that take char*
input parameters. You can also opt to use the =
operator to copy
data between Str
objects and char*
buffers if that is what works best for your
program.
When using the C++ I/O model, the main functions you will use are the
>>
and <<
stream operators. These functions make it
relatively easy to input and output string data. For example, to
capture a line of input directly into a Str
object, the command
is simply:
Str x; cin >> x;
When using the >>
operator the string will automatically grow as
needed to accommodate input data. The resize rules are the save as those
described in chapter 5, meaning that you can
start with a stack-based input buffer that will reallocate to the heap
automatically if required.
To output data in C++, the operator is <<
, just as you would
expect. Here is an example that prints a message to the screen. This
example mixes a number of types:
const char* hello = "Hello"; Str today = "today"; cout << hello << ", How are you " << today << "?" << endl;