![]() Chapter Contents |
![]() Previous |
![]() Next |
| SAS/C C++ Development System User's Guide, Release 6.50 |
The
class descriptions for the streams library are divided into three parts: description
of stream classes (such as
istream
and
ostream
), description of the buffer
classes
(such as
filebuf
and
streambuf
), and description of manipulators (
class IOMANIP
).
Most C++ programmers need only understand the stream classes. However, if you are doing more advanced programming, such as creating your own streams, you may want to also read the descriptions of the buffer and manipulator classes.
The protected interfaces in each of the classes have been implemented in accordance with AT&T Version Release 3.0 but are beyond the scope of this book to describe. For information on the protected interface for these classes, refer to your C++ book (such as the C++ Language System Release 3.0 Library Manual).
Note: It is not recommended that you use the protected member functions
in your applications. This interface is volatile and is quite likely to change
when the ANSI committee generates a C++ standard. Using the protected interface
in your applications makes them much more likely to be quickly obsolete.
This section provides descriptions
of the stream classes, such as
iostream
and
strstream
.
As noted previously, the protected
interface to these functions is not documented in the descriptions that follow.
Each class (or occasionally a set of related classes) is listed alphabetically.
All class descriptions include the following information:
The following classes are described in this section:
class fstream |
class strstream |
class ifstream |
class istrstream |
class ofstream |
class ostrstream |
class iostream |
class stdiostream |
class istream |
class ios |
class ostream |
class streampos |
class bsamstream |
class ibsamstream |
class obsamstream |
The
class ios
description is divided into several sections. One section
describes the basic stream-manipulation functions. The other sections deal
with the enumerations provided by
class ios
and the functions that
manipulate these enumerations. Examples of the enumerations include the format
flags, the I/O state flags, the open mode flags,
and the
seek_dir
flags.
Note: The term character in the following class and function
descriptions refers to either a
char
, a
signed char
, or an
unsigned char
.
#include <bsamstr.h>
class bsamstream : public iostream
{
public:
bsamstream();
bsamstream(const char *filename,
int mode,
const char *keywords = 0,
int willseek = 0,
bsam_exit_list *exit_list = 0);
virtual ~bsamstream();
void open(const char *filename,
int mode,
const char *keywords = 0,
int willseek = 0,
bsam_exit_list *exit_list = 0);
int find(const char *name);
int init_directory();
int delete_member(const char *name);
int rename_member
(const char *old_name,
const char *new_name);
int stow(const char *name,
char action = 'R',
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
int add_member(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
int replace_member(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
void close();
void setbuf(char *buffer,
size_t length);
int error_info(bsambuf::error_id& id);
void clear_error_info();
void set_user_data(void *user_data);
void *get_user_data();
const char *get_ddname();
const char *get_member();
bsambuf *rdbuf();
char dcbrecfm();
short dcblrecl();
short dcbblksize();
DCB_t *getdcb();
} ;
class ibsamstream : public istream
{
public:
ibsamstream();
ibsamstream(const char *filename,
int mode,
const char *keywords = 0,
int willseek = 0,
bsam_exit_list *exit_list = 0);
virtual ~ibsamstream();
void open(const char *filename,
int mode,
const char *keywords = 0,
int willseek = 0,
bsam_exit_list *exit_list = 0);
int find(const char *name);
int init_directory();
int delete_member(const char *name);
int rename_member
(const char *old_name,
const char *new_name);
int stow(const char *name,
char action = 'R',
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
int add_member(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
int replace_member(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
void close();
void setbuf(char *buffer,
size_t length);
int error_info(bsambuf::error_id& id);
void clear_error_info();
void set_user_data(void *user_data);
void *get_user_data();
const char *get_ddname();
const char *get_member();
bsambuf *rdbuf();
char dcbrecfm();
short dcblrecl();
short dcbblksize();
DCB_t *getdcb();
};
class ibsamstream : public istream
{
public:
ibsamstream();
ibsamstream(const char *filename,
int mode,
const char *keywords = 0,
int willseek = 0,
bsam_exit_list *exit_list = 0);
virtual ~ibsamstream();
void open(const char *filename,
int mode,
const char *keywords = 0,
int willseek = 0,
bsam_exit_list *exit_list = 0);
int find(const char *name);
int init_directory();
int delete_member(const char *name);
int rename_member
(const char *old_name,
const char *new_name);
int stow(const char *name,
char action = 'R',
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
int add_member(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
int replace_member(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
void close();
void setbuf(char *buffer,
size_t length);
int error_info(bsambuf::error_id& id);
void clear_error_info();
void set_user_data(void *user_data);
void *get_user_data();
const char *get_ddname();
const char *get_member();
bsambuf *rdbuf();
char dcbrecfm();
short dcblrecl();
short dcbblksize();
DCB_t *getdcb();
};<bsamstr.h>
header file, specialize the classes
iostream
,
istream
, and
ostream
for file I/O using a
bsambuf
.
class bsamstream
and
class obsamstream
define an identical set of functions.
class ibsamstream
omits the set
of functions that can change the file.class bsamstream
inherits characteristics from
class iostream
.
class ibsamstream
inherits characteristics from
class istream
.
class obsamstream
inherits characteristics from
class ostream
. All three of
class bsamstream
,
class ibsamstream
, and
class obsamstream
inherit characteristics from
class ios
. See the descriptions
of these parent classes for details on functions and operators that are inherited.
bsamstream::bsamstream()
ibsamstream::ibsamstream()
obsamstream::obsamstream()
bsamstream::bsamstream
(const char *filename,
int mode,
const char *keywords
= 0,
int willseek = 0,
bsam_exit_list *exit_list = 0)
ibsamstream::ibsamstream
(const char*filename,
int mode,
const char *keywords
= 0,
int willseek = 0,
bsam_exit_list *exit_list
= 0)
ibsamstream::ibsamstream
(const char *filename,
int mode,
const char *keywords
= 0,
int willseek = 0,
bsam_exit_list *exit_list
= 0)
filename
, using the specified
mode
. The
ibsamstream
behaves as if
ios::in
was
set in the
mode
argument, whether or not it was set by the caller. The
obsamstream
behaves as if
ios::out
was set in the
mode
argument, whether or not
it was set by the caller.
All of the arguments are identical to the corresponding
bsambuf::open
arguments. Refer to class bsambuf
for a description of these arguments.
If the open fails,
ios::badbit
is set in the stream's I/O state flags,
as described in enum io_state
.
class bsamstream
,
class ibsamstream
, and
class obsamstream
each have one destructor:
virtual bsamstream::~bsamstream()
virtual ibsamstream::~ibsamstream()
virtual obsamstream::~obsamstream()ios::failbit
is set in the stream's I/O state flags, as described in enum io_state
.bsambuf
arguments of the same name and function.
bsamstream::open(const char *filename,
int mode, const char *keywords
= 0,
int willseek = 0,
bsam_exit_list *exit_list
= 0)ibsamstream::open(const char
*filename,
int mode, const char *keywords
= 0,
int willseek = 0,
bsam_exit_list *exit_list
= 0)obsamstream::open(const char
*filename,
int mode, const char *keywords
= 0,
int willseek = 0,
bsam_exit_list *exit_list
= 0)filename
and using the specified
mode
. The
ibsamstream
constructor behaves as if
ios::in
was
set in the
mode
argument, whether or not it was set by the caller. The
obsamstream
constructor behaves as if
ios::out
was set in the
mode
argument, whether or not it was set by the caller.
If the open fails, the
ios::badbit
is set in the stream's I/O state flags,
as described in enum io_state
.
int bsamstream::find(const char *name)
int ibsamstream::find(const
char *name)
int obsamstream::find(const
char *name)name
is shorter than eight characters, it will be padded on the right
with blanks.
find
returns 0 if the file is successfully positioned or a value
other than 0 otherwise.int bsamstream::init_directory()
int obsamstream::init_directory()int bsamstream::delete_member
(const char *name)int obsamstream::delete_member
(const char *name)name
from the file connected to the
stream
.
name
may be specified in either upper- or lowercase. If
name
is shorter than eight characters, it will
be padded on the right with blanks.
The value returned is 0 if the member is successfully deleted or is a value
other than 0 otherwise.int bsamstream::rename_member
(const char *old_name,
const char *new_name)int obsamstream::rename_member
(const char *old_name,
const char *new_name)old_name
to
new_name
.
old_name
and
new_name
may be specified in either
upper- or lowercase. If either name is shorter
than eight characters, it will be padded on the right with blanks. The value
returned is 0 if the member is successfully renamed or is a value other than
0 otherwise.int bsamstream::stow(const char *name,
char action = 'R',
int user_data_length = 0,
const void *user_data = 0,
int alias = 0,
int TT = 0, int R = 0, int
TTRN = 0)int obsamstream::stow(const
char *name,
char action = 'R',
int user_data_length = 0,
const void *user_data = 0,
int alias = 0,
int TT = 0, int R = 0, int
TTRN = 0)bsambuf
. The file must be a PDS.
stow
returns a value other than
0 if any of the arguments are out of bounds. If
stow
invokes the STOW macro, with
one exception,
stow
returns the return code from the STOW macro.
(This code can also be retrieved via the
error_info
function.) In general,
the STOW macro returns 0 if the requested action succeeded or a value other
than 0 otherwise. The exception occurs when
action
is 'R' and the STOW macro
returns 8. This return code from the STOW macro indicates that the member
or alias did not previously exist and so was added. In this case,
stow
returns 0.
If an error occurs, the
ios::badbit
is set in the stream's
I/O state.
int bsamstream::add_member
(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);int obsamstream::add_member
(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);stow
function when
action
is
'A'. The arguments and return value are identical to
stow
.int bsamstream::replace_member
(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0)int obsamstream::replace_member
(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0)stow
function when
action
is
'R'. The arguments and return value are identical to
stow
.void bsamstream::close()
void ibsamstream::close()
void obsamstream::close()ios::failbit
is set in the stream's
I/O state. The close could fail if the call to
rdbuf()->close()
fails. These functions
have no return value.void bsamstream::setbuf(char *buffer,
size_t length)void ibsamstream::setbuf(char
*buffer,
size_t length)void obsamstream::setbuf(char
*buffer,
size_t length)rdbuf()->setbuf(buffer, length)
. These functions
have no return value.bsambuf *bsamstream::rdbuf()
bsambuf *ibsamstream::rdbuf()
bsambuf *obsamstream::rdbuf()bsambuf
associated with the stream.int bsamstream::error_info
(bsambuf::error_id& id)int ibsamstream::error_info
(bsambuf::error_id& id)int obsamstream::error_info
(bsambuf::error_id& id)bsambuf::error_info
for a list of values that can be stored in the
id
argument.void bsamstream::clear_error_info()
void ibsamstream::clear_error_info()
void obsamstream::clear_error_info()error_info
is called in this state, the value of
error_id
is
Enone
and the return code is 0.void bsamstream::set_user_data
(void *user_data)void ibsamstream::set_user_data
(void *user_data)void obsamstream::set_user_data
(void *user_data)user_data
for retrieval by
get_user_data
. The value may be any value required by the program. It is ignored
by the stream.void *bsamstream::get_user_data()
void *ibsamstream::get_user_data()
void *obsamstream::get_user_data()set_user_data
.void *bsamstream::get_ddname()
void *ibsamstream::get_ddname()
void *obsamstream::get_ddname()void *bsamstream::get_member()
void *ibsamstream::get_member()
void *obsamstream::get_member()DCB_t *bsamstream::getdcb()
DCB_t *ibsamstream::getdcb()
DCB_t *obsamstream::getdcb()char bsamstream::dcbrecfm()
char ibsamstream::dcbrecfm()
char obsamstream::dcbrecfm()short bsamstream::dcblrecl()
short ibsamstream::dcblrecl()
short obsamstream::dcblrecl()short bsamstream::dcbblksize()
short ibsamstream::dcbblksize()
short obsamstream::dcbblksize()class bsambuf, class bsam_exit_list
#include <fstream.h>
class ifstream : public istream
{
public:
ifstream();
ifstream(const char *name,
int mode = ios::in,
const char *amparms = "",
const char *am = "");
virtual ~ifstream();
void open(const char *name,
int mode = ios::in,
const char *amparms = "",
const char *am = "");
void close();
void setbuf(char *p, int len);
filebuf* rdbuf();
};
class ofstream : public ostream
{
public:
ofstream();
ofstream(const char *name,
int mode = ios::out,
const char *amparms = "",
const char *am = "");
virtual ~ofstream();
void open(const char *name,
int mode = ios::out,
const char *amparms = "",
const char *am = "");
void close();
void setbuf(char *p, int len);
filebuf* rdbuf();
};
class fstream : public iostream
{
public:
fstream();
fstream(const char *name, int mode,
const char *amparms = "",
const char *am = "");
virtual ~fstream();
void open(const char *name, int mode,
const char *amparms = "",
const char *am = "");
void close();
void setbuf(char *p, int len);
filebuf* rdbuf();
};fstream.h
header file specialize the classes
istream
,
ostream
, and
iostream
for
file I/O. In other words, the streambuf
associated with the I/O operations is a
filebuf
. The functions associated
with each of the classes in this header file,
class ifstream
,
class ofstream
, and
class fstream
,
are very similar.class ifstream
inherits characteristics from
class istream
.
class ofstream
inherits characteristics from
class ostream
.
class fstream
inherits characteristics from
class iostream
. All three of
class ifstream
,
ofstream
, and
fstream
inherit
characteristics
from
class ios
. See the descriptions of these parent classes for the details
on functions and operators that are inherited.class ifstream
,
class ofstream
, and
class fstream
, as follows:
ifstream::ifstream()
ofstream::ofstream()
fstream::fstream()ifstream::ifstream(const char
*name,
int mode = ios::in,
const char *amparms = " ",
const char *am = " ") ofstream::ofstream(const char *name,
int mode = ios::out,
const char *amparms = "",
const char *am = "")
fstream::fstream(const char *name,
int mode, const char *amparms
= "",
const char *am = "")name
, using the specified
mode
. The
ifstream
constructor behaves as if
ios::in
was set in the
mode
argument,
whether or not it was set by the caller. The
ofstream
constructor behaves as
if
ios::out
was set in the
mode
argument, whether or not it
was set by the caller.
An explanation of filename specification, as well as the arguments
amparms
and
am
can be found in the SAS/C Library Reference,
Volume 1. (Note that the order of the
amparms
and
am
, arguments
in these constructors is the opposite of the order in which they appear in
calls to the C
afopen
function.) You may also want to refer to
the SAS/C Compiler and Library User's Guide.
The available modes are described in enum open_mode . If the open fails, the stream's status is reflected in its I/O state flags, as described in enum io_state .
class ifstream
,
class ofstream
, and
class fstream
each have one destructor:
virtual ifstream::~ifstream()
virtual ofstream::~ofstream()
virtual fstream::~fstream()
close the stream, if opened.
void ifstream::open(const
char *name,
int mode = ios::in,
const char *amparms = " ",
const char *am = " ")void ofstream::open(const
char *name,
int mode = ios::out,
const char *amparms = " ",
const char *am = " ")void fstream::open(const char *name,
int mode,
const char *amparms = "",
const char *am = "")mode
.
ifstream::open()
behaves as if
ios::in
was set in the
mode
argument, whether or not it
was set by the caller.
ofstream::open()
behaves as if
ios::out
was set in the
mode
argument, whether or not it was set by the caller. For
fstream
objects
there is no default
open_mode
bit set.
The available open modes are described in enum open_mode . An explanation of the arguments
amparms
and
am
can be found in the SAS/C Library Reference, Volume 1.
(Note that the order of the
amparms
and
am
arguments in these functions
is the opposite of the order in which they appear in calls to the C
afopen
function.) You may also want to refer to the SAS/C Compiler
and Library User's Guide.
These functions do not have a return value. If an error occurs during
the open, the
ios::failbit
bit is set in the stream's I/O state. Reasons for a
failed open include that the file already exists, or that the call to
rdbuf()->open()
fails.
void ifstream::close()
void ofstream::close()
void fstream::close()rdbuf()->close()
fails. These functions have no return value. void ifstream::setbuf(char *p, int len)
void ofstream::setbuf(char *p, int len)
void fstream::setbuf(char *p, int len)filebuf::setbuf(p,len)
. These functions have no
return value. filebuf* ifstream::rdbuf()
filebuf* ofstream::rdbuf()
filebuf* fstream::rdbuf()filebuf
associated with the stream.class filebuf
#include <iostream.h>
class ios
{
public:
/* See the enum format_state, enum
io_state, enum open_mode, and enum
seek_dir descriptions for more
definitions. */
ios(streambuf *buf);
virtual ~ios();
int width();
int width(int w);
char fill();
char fill(char c);
int precision();
int precision(int i);
static unsigned long bitalloc();
static int xalloc();
long& iword(int i);
void*& pword(int i);
streambuf* rdbuf();
ostream* tie();
ostream* tie(ostream *s);
};iostream.h
header file declares
class ios
.
This class and the classes derived from it provide an I/O interface for inserting
information into and extracting information from
streambuf
. This I/O interface supports
both formatted and unformatted
information. This description is devoted to those operations used in stream
and buffer manipulation.
Several enumerations are defined in
class ios
(
io_state
,
open_mode
,
seek_dir
, and the format flags). These enumerations
are described in subsequent sections. The
open_mode
and
seek_dir
flags
are not used directly by the functions in
class ios
but are used by classes
derived from it.
class ios
is the parent of all the stream classes.
It has no parent class.class ios
defines one constructor:
ios::ios(streambuf *buf)buf
as the associated
streambuf
. If
buf
is
NULL
, the effect is undefined.class ios
destructor:
virtual ios::~ios()class ios
defines several functions that provide
buffer and stream manipulation capabilities. The following list describes
these functions.
streambuf* ios::rdbuf()streambuf
associated with the
ios
when it was created.ostream* ios::tie()ostream
currently tied to the
ios
, if any; returns
NULL
otherwise.ostream* ios::tie(ostream *s)s
to the
ios
and returns the stream previously
tied to this stream, if any; returns
NULL
otherwise.
If the
ios
is tied to an
ostream
, then the
ostream
is flushed before every
read or write from the
ios
. By default,
cin
,
cerr
, and
clog
are tied
to
cout
.
class ios
defines several functions that use and
set the format flags and variables.
class ios
also provides functions
you can use to define and manipulate your own formatting flags, plus several
built-in manipulators that allow you to set various format flags.flags()
,
setf()
, and
unsetf()
functions
and the format flags are described in enum format_state
.
int ios::width()int
representing the value of the current field
width.int ios::width(int w)w
and returns an
int
representing
the previous field width value.
The default field width is 0. When the field width is 0, inserters insert only as many characters as necessary to represent the value. When the field width is nonzero, inserters insert at least as many characters as are necessary to fill the field width. The fill character is used to pad the value, if necessary, in this case.
Numeric inserters do not truncate their values. Therefore, if the value being inserted is wider than the field width, the entire value is inserted, regardless of the field width overrun. As you can see, this implies that the field width value is a minimum constraint; there is no way to specify a maximum constraint on the number of characters to be inserted.
The field width variable is reset to 0 after each insertion or extraction. In this sense, the field width serves as a parameter for insertions and extractions.
You can also use the parameterized manipulator,
setw
, to set
the field width.
char ios::fill()char ios::fill(char c)c
and returns a
char
representing the previous value. The default
fill character is a space. You can also set the fill character using the parameterized
manipulator,
setfill
.int ios::precision()int
representing the current precision value.int ios::precision(int i)i
and returns an
int
representing
the previous precision. Use this function to control the number of significant
digits included in floating-point values. The default precision is six. You
can also set the precision using the parameterized manipulator,
setprecision
.class ios
includes four functions that you can
use to define format flags and variables in addition to those described in
enum format_state .
static unsigned long ios::bitalloc()unsigned long
with a single, previously unallocated,
bit set. This allows you to create additional format flags. This function
returns 0 if there are no more bits available. Once the bit is allocated,
you can set it and clear it using the
flags()
,
setf()
, and
unsetf()
functions.static int ios::xalloc()int
that represents a previously unused index into
an array of words available for use as format state variables. These state
variables can then be used in your derived classes.long& ios::iword(int i)i
th user-defined word.
i
must be
an index allocated by
ios::xalloc()
.void*& ios::pword(int i)i
th user-defined word.
i
must be
an index allocated by
ios::xalloc()
.
pword()
is the same as
iword()
except
that its return type is different.ios&
, an
istream&
, or an
ostream&
and return their
argument. The following built-in manipulators are useful with
ios
objects.
stream
has type
ios&
.
stream >> dec
and
stream << decios::dec
bit and clearing
ios::oct
and
ios::hex
).stream >> oct
and
stream << octios::oct bit
and clearing
ios::dec
and
ios::hex
).stream >> hex
and
stream << hexios::hex
bit and clearing
ios::dec
and
ios::oct
).stream >> wsstream << endlstream << ends\0
) character into the stream.stream << flushostream
object.In addition, parameterized manipulators are available to operate on
ios
objects. These include
setfill
,
setprecision
,
setiosflags
, and
resetiosflags
, described in detail in class IOMANIP .
class iostream, class istream, class ostream
#include <iostream.h>
class ios
{
public:
/* See the class ios, enum io_state,
enum open_mode, and enum seek_dir
descriptions for more definitions. */
enum {skipws,
left,
right,
internal,
dec,
oct,
hex,
showbase,
showpoint,
uppercase,
showpos,
scientific,
fixed,
unitbuf,
stdio
};
static const unsigned long basefield;
static const unsigned long adjustfield;
static const unsigned long floatfield;
unsigned long flags();
unsigned long flags(unsigned long f);
unsigned long setf(unsigned long mask);
unsigned long setf(unsigned long
setbits, unsigned
long mask);
unsigned long unsetf(unsigned long
mask);
};class ios
(defined in the
iostream.h
header file) provides a format state, which is used by the stream classes
to control formatting. The format state is controlled by the format flags,
and
class ios
provides several functions to manipulate
these flags. The member functions
flags()
,
setf()
, and
unsetf()
control
the majority of formatting. These functions are described further on in this
section. Other member functions that have an effect on the format state are
fill()
,
width()
, and
precision()
. These functions are
described in the previous
class ios
description.
In addition to the predefined format flags, users can create their own
user-defined format flags. This is described in the previous
class ios
description, under "User-defined format flag functions".
skipwsskipws
is not set, whitespace is not skipped.
To protect against looping, zero-width fields are considered a bad format.
Therefore, if the next character is whitespace and
skipws
is not set, arithmetic extractors
signal an error.
skipws
is set by default.
leftrightright
is set by default.internaladjustfield
. To set the fill character, use the
fill()
function.
To control the width of formatted items, use the
width()
function.These conversion base flags are grouped together by the member
basefield
.
Although decimal is the default conversion base for insertions (if none
of these flags is set), the default conversion base for extractions follows
the C++ lexical conventions for integral constants. You can also use the built-in
manipulators
dec
,
oct
, and
hex
to control the conversion base. These manipulators
are described in the previous
class ios
description.
showbaseshowbase
is not set by default.showpointshowpoint
is not set by default.uppercaseX
is used instead of
x
in hexadecimal output, and an
E
is used instead of
e
in scientific
notation.
uppercase
is not set by default.showpos+
sign to be added to the decimal conversion of positive integers.
showpos
is not set by default.
scientificprecision()
function). The default precision is six. An
e
(or
E
if
uppercase
is set) precedes the exponent.fixedprecision()
function. The default
precision is six.
If neither
scientific
nor
fixed
is set, the value is converted
to one or the other format, according to the following rules:
showpoint
is set, trailing zeros are removed from
the value, regardless of the format. A decimal point appears in the value
only if it is followed by a digit. These flags are grouped together by the
member
floatfield
. They are not set by default.unitbufunitbuf
is not set by default.stdiostdout
and
stderr
to be flushed after an insertion.
stdio
is not set by default.
unsigned long ios::flags()unsigned long
representing the current format flags.unsigned long ios::flags
(unsigned long f)f
and returns an
unsigned long
representing the previous flag values.unsigned long ios::setf
(unsigned long mask)mask
and returns an
unsigned long
representing the previous values
of those flags. You can accomplish the same task by using the parameterized
manipulator,
setiosflags
.unsigned long ios::setf
(unsigned long setbits, unsigned long mask)mask
according
to the corresponding values specified by
setbits
and returns an
unsigned long
representing the previous values of the bits specified by
mask
. The
EXAMPLES section provides an example of using this function.
Using
setf(0, mask)
clears all the bits specified by
field
. You
can accomplish the same task by using the parameterized manipulator
resetiosflags
.
unsigned long ios::unsetf
(unsigned long mask)mask
and returns
an
unsigned long
representing the previous flag values.setf()
function is used to change format flags.
For example, if you want to change the conversion base in an
ios
object
called
s
, you could use the following expression:
s.setf(ios::hex, ios::basefield)
In this example,
ios::basefield
represents the conversion base bits
you want to change, and
ios::hex
is the new value.
To set a flag that is not part of a field, use
setf()
with
a single argument, as in the following example, which sets the
skipws
flag:
s.setf(ios::skipws)
To clear the
skipws
flag, use
unsetf()
:
s.unsetf(ios::skipws)
As another example of using
setf()
, suppose you want to clear
in your
ios
object
s
all the bits specified by the variable
clearbits
. You could use the following expression
to accomplish this:
s.setf(0, clearbits)
#include <iostream.h>
class ios
{
public:
enum io_state {goodbit = 0,
eofbit,
failbit,
badbit
};
/* See the class ios, enum format_state,
enum open_mode, and enum seek_dir
descriptions for more definitions. */
int rdstate();
int eof();
int fail();
int bad();
int good();
void clear(int i = 0);
operator void*();
int operator !();
};class ios
(defined in the
iostream.h
header file) defines
io_state
flags that represent the internal state
of an
ios
object. Each flag has a value that can
be set or reset independently for an
ios
object. Note that
goodbit
is
not a flag but rather a symbolic name for the condition in which no flags
are set. The functions such as
rdstate()
and
eof()
use
and manipulate the I/O state flags.io_state
enumerators are as follows:
class ios
also provides several functions that
use or manipulate the I/O state flags. In addition to the I/O state functions,
class ios
also defines two operators that allow you to check the
I/O state of an
ios
object.
The following functions use and manipulate the values of the I/O state flags.
int ios::rdstate()int ios::eof()eofbit
if
eofbit
is
set; otherwise, returns 0.int ios::fail()failbit
if
failbit
is
set; otherwise, returns 0.int ios::bad()badbit
if
badbit
is
set; otherwise, returns 0.int ios::good()void ios::clear(int i = 0)i
. The default value for
i
is 0.
clear()
has no return value.The following two operators are useful when checking the I/O state of
an
ios
object.
ios::operator void*()converts an
ios
object to a pointer. If no bits
are set in the stream's I/O state, this operator returns a pointer value that
is not null. If
failbit
or
badbit
is set, the operator returns
0.
int ios::operator !()converts an
ios
object to 0 if no bits are set
in the stream's I/O state, or to a nonzero value if any bits are set in the
stream's I/O state.
#include <iostream.h>
class ios
{
public:
/* See the class ios, enum format_state,
enum io_state, and enum seek_dir
descriptions for more definitions. */
enum open_mode {in,
out,
ate,
app,
trunc,
nocreate,
noreplace,
binary
};
};open_mode
enumeration is defined in
iostream.h
. This enumeration defines a number of flags that can be used when creating
or opening a stream to specify attributes of the stream. You can specify several
attributes simultaneously by using the OR operator to combine them. For example,
to specify both the
out
and
binary
flags, use
ios::out|ios::binary
.
Only the
ios::ate
and
ios::app
flags are meaningful for
string streams, such as
strstream
objects. See the description of
class strstream
,
class istrstream
, and
class ostrstream
for information
on how these flags are used with these classes.
The following list describes the meaning of the
open_mode
flags for the file-oriented stream classes:
inoutios::in
,
ios::ate
, or
ios::app
is
also specified.ateapptruncios::trunc
has no effect if the file does not yet exist.nocreatenoreplacebinaryios::binary
is not specified, the file is accessed in text mode. See the SAS/C
Library Reference, Volume 1 for more information on the differences
between text mode and binary mode.
#include <iostream.h>
class ios
{
public:
/* See the class ios, enum format_state,
enum open_mode, and enum open_mode
descriptions for more definitions. */
enum seek_dir {beg, cur, end};
};class ios
(defined in the
iostream.h
header file) provides the
seek_dir
flags to control seeking. The following
list describes these flags:
beg |
means the seek is relative to the beginning of the stream. |
cur |
means the seek is relative to the current position of the stream. |
end |
means the seek is relative to the end of the stream. |
#include <iostream.h>
class iostream : public ostream,
public istream
{
public:
iostream(streambuf *buf);
};iostream.h
header file also provides
class iostream
, which is both an
istream
and an
ostream. class iostream
includes
all the operations of both subclasses.
It adds only a constructor of its own.class iostream
inherits characteristics from both
class istream
and
class ostream
. See the descriptions of these parent
classes for the details on functions and operators that are inherited.class iostream
defines one constructor:
iostream::iostream(streambuf *buf)buf
as the associated
streambuf
. If
buf
is
NULL
, the effect is undefined.class ios
,
class istream
,
class ostream
#include <iostream.h>
class istream : virtual public ios
{
public:
istream(streambuf *buf);
virtual ~istream();
int ipfx(int need = 0);
istream& operator >>(char *str);
istream& operator >>(unsigned char *str);
istream& operator >>(signed char *str);
istream& operator >>(char& c);
istream& operator >>(unsigned char& c);
istream& operator >>(signed char& c);
istream& operator >>(short& sh);
istream& operator >>(unsigned short& sh);
istream& operator >>(int& i);
istream& operator >>(unsigned int& i);
istream& operator >>(long& l);
istream& operator >>(unsigned long& l);
istream& operator >>(float& f);
istream& operator >>(double& d);
istream& operator >>(long double& ld);
istream& operator >>(streambuf *buf);
istream& operator >>(istream&(*f)
(istream&));
istream& operator >>(ios&(*f)(ios&));
istream& get(char *str, int len,
char delim = '\n');
istream& get(unsigned char *str,
int len,
char delim = '\n');
istream& get(signed char *str,
int len,
char delim = '\n');
istream& get(signed char& c);
istream& get(unsigned char& c);
istream& get(char& c);
istream& get(streambuf& sb,
char delim = '\n');
int get();
istream& getline(char *str, int len,
char delim = '\n');
istream& getline(unsigned char *str,
int len,
char delim = '\n');
istream& getline(signed char *str,
int len,
char delim = '\n');
istream& ignore(int n = 1,
int delim = EOF);
istream& read(char *str, int n);
istream& read(unsigned char *str,
int n);
istream& read(signed char *str,
int n);
int gcount();
int peek();
istream& putback(char c);
int sync();
istream& seekg(streampos pos);
istream& seekg(streamoff offset,
seek_dir place);
streampos tellg();
};
istream& ws(istream&);class istream
is defined in the
iostream.h
header file and is the base class for those stream classes that support only
input. It includes all the basic extraction functions (formatted input) on
fundamental C++ types, as well as a number of unformatted input functions
and several functions that enable you to move the get pointer.
It also includes one manipulator. These members are described in the following
sections.class istream
inherits characteristics from
class ios
. See the description of this parent class for the details on functions
and operators that are inherited.class istream
defines one constructor:
istream::istream(streambuf *buf)istream
and associates a
streambuf
with it.
class istream
destructor:
virtual istream::~istream()istream
.class istream
defines an input prefix function
that performs those operations that must be done before each formatting operation.
This function is defined as follows:
If any I/O state flags are set for theint istream::ipfx(int need = 0);
istream
, this function returns 0 immediately. If necessary, it flushes the
ios
(if any) tied to this
istream
. Flushing is necessary if
need
is 0
or if there are less than
need
characters available for input.
If
ios::skipws
is set and
need
is 0, then this function causes
any leading white space in the input to be skipped. If an error occurs during
this skipping,
ipfx()
returns 0. If no errors have occurred, this
function returns 1.
This function is called by all formatted extraction operations and should
be called by user-defined extraction operators unless the first input operation
used by the user-defined extraction operator is a formatted extraction. For
user-defined operations,
ipfx()
should be called with the argument equal
to 0.
operator >>
are called extraction
operators. They are formatted input functions. They each call the input prefix
function
ipfx(0)
and do nothing else if it returns 0. If
ipfx(0)
does
not return 0, the formatted input functions
extract leading characters from the associated
streambuf
according to the type
of their argument and the formatting flags in the
ios
. They all return the address
of the
istream
.
Errors during extraction are indicated by setting the appropriate I/O state flags for the stream, as follows:
ios::failbitios::badbitstreambuf
.Here are the functions.
istream& istream::operator >>(char *str) istream& istream::operator >>(unsigned char *str) istream& istream::operator >>(signed char *str)width()
is
nonzero, these functions extract no more than
width() - 1
characters and reset
width()
to 0. These functions add
a terminating null character, even if an error occurs during extraction.istream& istream::operator >>(char& c)
istream& istream::operator >>(unsigned char& c)istream& istream::operator >>(signed char& c)istream& istream::operator >>(short& sh)istream& istream::operator >>(unsigned short& sh)
istream& istream::operator >>(int& i)istream& istream::operator >>(unsigned int& i)istream& istream::operator >>(long& l)istream& istream::operator >>(unsigned long& l)ios::dec,
ios::oct,
or
ios::hex is set in the formatting state, characters are extracted
and converted according to the bit that is set.
If none of these bits is set, then these functions expect any of the following
formats:
0Xhhh
0ooo
ddd
Extraction stops when it reaches an unacceptable character. The acceptable characters are
ios::failbit
is set if no digits are found.istream& istream::operator >>(float& f)istream& istream::operator >>(double& d)istream& istream::operator >>(long double& ld)ios::failbit
is set if there are no digits to extract or if the format
is not correct.istream& istream::operator >>(streambuf *buf)
istream
and inserts them into the
streambuf. Extraction stops when no more characters can be obtained
from the
istream. istream& istream::operator >> (istream& (*f) (istream&))
istream& istream::operator >> (ios& (*f) (ios&))ios
or
istream
argument in some manner.ipfx(1)
first and do nothing else if 0 is returned.
istream& istream::get(char *str, int len,
char delim = '\n')
istream& istream::get(unsigned char *str,
int len, char delim = '\n')
istream& istream::get(signed char *str,
int len, char delim = '\n')len - 1
characters. Extraction stops
when a
delim
character
is extracted, when no more characters are available, or when
len - 1
characters
have been found. These functions store a terminating null character in the
array.
ios::failbit
is set only if no characters at all were extracted.istream& istream::getline(char *str,
int len, char delim = '\n')
istream& istream::getline
(unsigned char *str, int
len,
char delim = '\n')istream& istream::getline
(signed char *str, int len,
char delim = '\n')get()
functions, except that the terminating
delim
character (if found) is extracted. A
terminating null character is always stored in the array.istream& istream::get(streambuf& sb, char delim = '\n')sb
.
delim
is not extracted
or inserted.
ios::failbit
is set if an error occurs while inserting into
sb
.istream& istream::get(signed char& c)
istream& istream::get(unsigned
char& c)
istream& istream::get(char& c)ios::failbit
is set if no characters
can be extracted.int istream::get()ios::failbit
is never set.istream& istream::ignore(int n = 1, int delim = EOF)n
characters or up to the next
delim
character.
ios::failbit
is never set.istream& istream::read(char *str, int n)
istream& istream::read(unsigned char *str,
int n)
istream& istream::read(signed char *str,
int n)n
characters and store them into
the array pointed to by
s
.
is set if fewer thanios::failbit
n
characters can be extracted.class istream
includes several other functions,
as follows:
int istream::gcount()int istream::peek()ipfx(1)
returns 0 or if no characters
remain to be extracted. Otherwise it returns the next character in the stream
without extracting it.istream& istream::putback(char c)c
so it will be the next character extracted.
c
must be
the same as the last character extracted from the istream.
ios::badbit
is set if the streambuf cannot push
c
back.int istream::sync()sync()
on the associated
streambuf
.
This function returns whatever the
streambuf::sync()
call returned.istream& istream::seekg(streampos pos)istream& istream::seekg(streamoff offset,
seek_dir place)streambuf.pos
is a value returned by a previous call to tellg(). offset and place are
explained in the
streambuf::seekoff()
.streampos istream::tellg()streampos
of the get pointer of
the associated
streambuf
.
istream& ws(istream&);class ios, class iostream, class ostream
#include <iostream.h>
class ostream : virtual public ios
{
public:
ostream(streambuf *buf);
virtual ~ostream();
int opfx();
void osfx();
ostream& operator <<(char c);
ostream& operator <<(signed char c);
ostream& operator <<(unsigned char c);
ostream& operator <<(const char *str);
ostream& operator <<(const unsigned
char *str);
ostream& operator <<(const signed
char *str);
ostream& operator <<(short sh);
ostream& operator <<(unsigned short sh);
ostream& operator <<(int i);
ostream& operator <<(unsigned int i);
ostream& operator <<(long l);
ostream& operator <<(unsigned long l);
ostream& operator <<(float f);
ostream& operator <<(double d);
ostream& operator <<(void *vp);
ostream& operator <<(streambuf *buf);
ostream& operator <<(ostream&(*f)
(ostream&));
ostream& operator <<(ios&(*f)(ios&));
ostream& put(char c);
ostream& write(const char *str, int n);
ostream& write(const signed char *str,
int n);
ostream& write(const unsigned char *str,
int n);
ostream& flush();
streampos tellp();
ostream& seekp(streampos pos);
ostream& seekp(streamoff offset,
seek_dir place);
};
ostream& endl(ostream&);
ostream& ends(ostream&);
ostream& flush(ostream&);class ostream
is declared in the
iostream.h
header file and is the base class for those classes that support only output.
It includes all the basic insertion operators (formatted output) on fundamental
C++ types, as well as a number of unformatted output functions and functions
designed to change the stream position. In addition,
some output manipulators are defined for use with this class.class ostream
inherits characteristics from
class ios
. See the description of this parent class for the details on functions
and operators that are inherited.class ostream
defines one constructor:
ostream::ostream(streambuf *buf)ostream
and associates a
streambuf
with it.class ostream
destructor:
virtual ostream::~ostream()ostream
. The prefix operations are
done by
ostream::opfx()
and the suffix operations are done by
ostream::osfx()
.
int ostream::opfx()ostream
. This
function returns 0 and does nothing else if any bits in the stream's I/O state
are set; it returns 1 otherwise. If the
ostream
is tied (see
tie()
) to
another, the other stream is flushed (see
flush()
).
By convention,
opfx()
is called before any formatted output operation
on a stream. If it returns 0 (meaning one or more bits are set in the stream's
I/O state), the output operation is not performed. Each of the built-in inserters
follows this convention. User-defined formatted output functions should also
follow this convention by calling this function and checking the return code
before doing any output.
void ostream::osfx()ios::unitbuf
is set, this
ostream
is flushed. If
ios::stdio
is set,
cout
and
cerr
are flushed. This function should be called at the end of any
formatted output function that does unformatted output on the
ostream
. It
need not be called if the last output operation on the
ostream
was
formatted.operator <<
are called inserters
(because they insert values into the output stream). All inserters are formatted
output operations and as such follow the formatted output conventions mentioned
previously.
All of the inserters do the following: First, they call
opfx()
, and
if it returns 0, they do nothing else. If there is no error, they then convert
the input argument to a converted value (a sequence of characters), based
on the argument's type and value and on the formatting flags set for the stream.
The rules for construction of the converted value are given here for each
inserter.
Once a converted value has been determined, it is copied, possibly with
the addition of fill characters, to an output field. The characters of the
output field are then inserted into the stream's buffer. The
ios::width()
function for the stream determines the minimum number of characters in the
output field. If the converted value had fewer characters, fill characters
(defined by the value of
ios::fill()
for the stream) are added to pad out
the field. The placement of fill characters is as follows::
ios::right ios::left ios::internalNote that truncation cannot occur when copying the converted value to
an output field, regardless of the value of
width()
.
Once the converted value is constructed and the field is padded to be
at least
ios::width()
characters wide,
ios::width()
is reset to 0 and
osfx()
is called. All inserters indicate errors by setting I/O state
flags in the
ostream
, as necessary. Inserters always return a reference to their
ostream
argument.
Here are the formatted output functions.
ostream& ostream::operator <<(char c)ostream& ostream::operator <<(signed
char c)ostream& ostream::operator <<(unsigned
char c)char c
.ostream& ostream::operator <<(const
char *str)
ostream& ostream::operator <<(const
unsigned char *str)
ostream& ostream::operator <<(const
signed char *str)chars
, up
to but not including a '
\0
' character, pointed to by
str
.ostream& ostream::operator <<(short sh)ostream& ostream::operator <<(unsigned
short sh)
ostream& ostream::operator <<(int i)
ostream& ostream::operator <<(unsigned
int i)
ostream& ostream::operator <<(long l)ostream& ostream::operator <<(unsigned
long l)-
' if the argument is negative.
If the following format flags within the
ostream
are set, they affect the
converted value as follows:
ios::showpos+
' to be included in the converted value if the value is positive.ios::dec, ios::oct, and
ios::hex ios::showbaseIf both a sign representation (+ or -) and a base representation appear in the converted value, the sign appears first.
ostream& ostream::operator <<(float f)
ostream& ostream::operator <<
(double d)These formats are described in detail in enum format_state . The format of the converted value is affected by the settings of the following format flags:
ios::fixed
or
ios::scientific ios::showpoint showpoint
is not set and all digits after the decimal point are
zero, these digits and the decimal point are dropped. ios::uppercasee
' in scientific notation to be
'
E
' instead. ios::showposostream& ostream::operator <<
(void *vp)vp
to an
unsigned long
and represents it as if
ios::hex
and
ios::showbase
were set.ostream& ostream::operator <<
(streambuf *buf) buf
and inserts them into the output
stream, provided no bits are set in
buf
's I/O state. No padding is
done. If any bits are set in
buf
's I/O state, this function returns immediately.ostream& ostream::operator <<
(ostream&(*f) (ostream&))
ostream& ostream::operator <<
(ios&(*f)(ios&))ios
or
ostream
argument in some manner.opfx()
and
osfx()
; however, these functions do check to see whether any I/O
state flags are set for the
ostream
and take no further action if any are found. All inserters indicate errors
by setting I/O state flags in the
ostream
. Each of these functions
returns a reference to its argument
ostream
.
ostream& ostream::put(char c)ostream& ostream::write(const char *str,
int n)ostream& ostream::write(const signed
char *str, int n)
ostream& ostream::write(const unsigned
char *str, int n)n
characters starting at
str
into the
stream. The characters are treated as plain
chars
independent of their actual
type. The null character is treated the same as any other character.class ostream
.
ostream& ostream::flush()rdbuf()->sync()
. For more information, refer to
the description of
streambuf::sync()
.streampos ostream::tellp()streambuf::seekoff()
and
streambuf::seekpos()
. ostream& ostream::seekp(streampos pos)
ostream& ostream::seekp(streamoff offset,
seek_dir place)streambuf::seekoff()
and
streambuf::seekpos()
.
ostream& endl(ostream&)\n
' character into the stream. Here is an example:
#include <iostream.h>
float mynum=3.2;
// Writes "mynum is:" and the value
of mynum on one line.
cout << "mynum is: " << mynum << endl;ostream& ends(ostream&)\0
' character into the stream. Here is an example,
using a
strstream
:
#include <iostream.h> strstream mystream; float mynum=3.2; // Writes mynum to mystream. mystream << "mynum is: " << mynum << ends;
ostream& flush(ostream&)ostream.flush()
.class ios, class iostream, class istream
#include <stdiostream.h>
class stdiostream : public iostream
{
public:
stdiostream(FILE *file);
FILE* stdiofile();
stdiobuf* rdbuf();
}; class stdiostream
is declared in the
stdiostream.h
header file. It provides
iostream
access to an external file accessed by
C functions using the ANSI standard I/O interfaces declared in
stdio.h
.
Use of
class stdiostream
enables a program to use stdio output and C++
iostream
output in the same output file. Similarly,
class stdiostream
enables your program to use
stdio
input and C++
iostream
input to process the same input file.class stdiostream
inherits characteristics from
class iostream
, which in turn inherits characteristics from
class istream
,
class ostream
, and
class ios
. See the descriptions of these parent
classes for the details on functions and operators that are inherited.class stdiostream
has one constructor:
stdiostream::stdiostream(FILE *file)FILE* file
. The constructor assumes
that the file is open.
stdiostream::FILE* stdiofile()FILE*
associated with this stream.stdiobuf* stdiostream::rdbuf()stdiobuf
associated with the stream.class stdiobuf
#include <iostream.h>
class streampos
{
public:
streampos();
streampos(long n);
operator long();
fpos_t* fpos();
};class streampos
is declared in the
iostream.h
header file and is used to record or specify a position in a stream. This
class is for use only with streams that support seeking.
Most
streampos
values are similar to C
fpos_t
values; that is, they record
file position values in a way private to the implementation. Because these
values are probably not useful for user-defined stream classes, it is also
possible to create integral-valued
streampos
objects. Note that integral-valued
streampos
objects probably are not useful for positioning
fstream
or
stdiostream
objects.
streampos::streampos()streampos
object with an unknown value.streampos::streampos(long n)streampos
object from the value
n
. All kinds
of stream buffers support the following values of
n
:
streampos(0)streampos(EOF)strstream
objects created from other values of
n
are not
useful for positioning
fstream
or
stdiostream
objects.
streampos::operator long()streampos
to a long integer. The
value of
long(streampos(long_val))
is defined to be equal to
long_val
.
The result of converting a
streampos
not constructed from a
long
is undefined.fpos_t* streampos::fpos()fpos_t
contained in the
streampos
. This
fpos_t
contains a valid value only if this
streampos
was returned from a call to
seekoff()
or
seekpos()
on a
filebuf
or
stdiobuf
object.
#include <strstream.h>
class istrstream : public strstreambuf,
public istream
{
public:
istrstream(char *str);
istrstream(char *str, int size);
~istrstream( );
strstreambuf* rdbuf ( );
};
class ostrstream : public strstreambuf,
public ostream
{
public:
ostrstream(char *str, int size,
int mode = ios::out);
ostrstream( );
~ostrstream( );
char* str( );
int pcount ( );
strstreambuf* rdbuf( );
};
class strstream : public strstreambuf,
public iostream
{
public:
ostrstream(char *str, int size,
int mode = ios::out);
ostrstream( );
~ostrstream( );
char* str( );
int pcount( );
strstreambuf* rdbuf( );
};
class strstream : public strstreambuf,
public iostream
{
public:
strstream(char *str, int size,
int mode = ios::out);
strstream( );
~strstream( );
char* str( );
int pcount( );
strstreambuf* rdbuf( );
};class strstream
and its associated classes
class istrstream
and
class ostrstream
are declared in the
strstream.h
header file. These classes
support string
(array) I/O. They do this by customizing the I/O operations defined in the
base classes
istream
,
ostream
, and
iostream
.class istrstream
inherits characteristics from
class istream
.
class ostrstream
inherits characteristics from
class ostream
, and
class strstream
inherits characteristics from
class iostream
. All three of
class istrstream
,
ostrstream
, and
strstream
inherit characteristics
from
class ios
. See the descriptions of these parent classes for the details
on functions and operators that are inherited.class strstream
,
class istrstream
, and
class ostrstream
each have two constructors. For a discussion of dynamic mode versus static
mode streams, see class strstreambuf
.
istrstream::istrstream(char *str)
istrstream
such that extraction
operations on the stream will fetch the characters of
str
, up to
the terminating '
\0
'.
str
must be
null-terminated. The '
\0
' character is not fetched. Seeks are allowed
within the array.istrstream::istrstream(char *str,
int size)istrstream
such that extraction
operations on the stream will fetch characters from the array starting at
str
and extending for
size
bytes. Seeks are allowed within
the array.ostrstream::ostrstream(char *str,
int size, int mode = ios::out)ostrstream
referencing an area of
size
bytes starting at the character pointed to by
str
. The get
pointer is positioned at the beginning of the array. The put pointer is also
positioned at the beginning of the array
unless either the
ios::ate
or
ios::app
bit is set in
mode
; if either
of these bits is set, the put pointer is positioned at the space that contains
the first null character. Seeks are allowed anywhere within the array.ostrstream::ostrstream()ostrstream
. This involves dynamically
allocating space to hold stored characters. Seeks are not allowed.strstream::strstream(char *str,
int size, int mode = ios::out)strstream
referencing an area of
size
bytes starting at the character pointed to by
str
. The get
pointer is positioned at the beginning of the array. The put pointer is also
positioned at the beginning of the array unless
either the
ios::ate
or
ios::app
bit is set in
mode
; if either of these bits is
set, the put pointer is positioned at the space that contains the first null
character.strstream::strstream()strstream
. This involves allocating
space to hold stored characters. Seeks are not allowed. The get pointer is
positioned at the beginning of the array.class istrstream
,
class ostrstream
, and
class strstream
each have one destructor:
istrstream::~istrstream()
ostrstream::~ostrstream()
strstream::~strstream()class istrstream
,
class ostrstream
, and
class strstream
.
char* ostrstream::str()
char* strstream::str()str()
on the associated
streambuf
. These functions return
whatever the
streambuf::str()
call returned.int ostrstream::pcount()
int strstream::pcount()strstreambuf* istrstream::rdbuf()
strstreambuf* ostrstream::rdbuf()
strstreambuf* strstream::rdbuf()strstreambuf
associated with the
stream.strstream
, inserts a string and
a number, then extracts them again, writing the contents of
mystream
to
cout
.
strstream mystream; float mynum=3.2; float num2; // Write mynum to mystream. mystream << mynum << ends; // Extract the contents of mystream // and store them in num2. mystream >> num2; // Get the string from mystream and // write it to cout. cout << mystream.str();
class strstreambuf
This section provides the descriptions of the buffer classes, such as
filebuf
and
strstreambuf
.
Using these classes is an advanced programming technique and is not required for simple C++ programs that use I/O. As with the stream class descriptions, the protected interface to the buffer classes is not included in the class descriptions that follow, although it is implemented.
Each class (or occasionally, a set of related classes) is listed alphabetically. All class descriptions include the following information:
In addition, some class descriptions contain other sections, as appropriate for the class.
The following classes are described in this section:
class filebuf class stdiobuf class streambuf class strstreambuf class bsambuf class bsam_exit_list |
The
bsambuf
class is a specialized version of
class streambuf
and was added with
Release 6.00. It implements I/O via the record-oriented Basic Sequential Access
Method (BSAM) interface of the SAS/C OS Low-Level I/O functions. In addition
to providing all the functionality of the
streambuf
class,
class bsambuf
permits functions to be called as BSAM exits via objects of
class bsam_exit_list
.
The
bsambuf
class is defined in the header file
<bsamstr.h>
. This header file also defines a set of classes for performing formatted
file I/O using a
bsambuf
object. These include:
These classes are specialized versions of class
iostream
,
istream
, and
ostream
, respectively.
For details about the streams library and the parent class of
class bsambuf
, refer to class streambuf
in the "I/O Class Descriptions" section. For details about the
OS Low-Level I/O functions, refer to SAS/C Library Reference, Third
Edition, Volume 2. For more information about BSAM, refer to the IBM
publication MVS/XA Data Administration
Guide (GC26-4140).
Note: The classes defined in
<bsamstr.h>
can be used in a
C++ program that uses the Systems Programming Environment (SPE) library.
Note: The term character in the following class and function
descriptions refers to either a
char
, a
signed char
, or an
unsigned char
.
class bsambuf : public streambuf
{
public:
enum error_id { Enone,
Estorage,
Eabend,
Eopen,
Eclose,
Eget,
Eput,
Efind,
Estow,
Eflush,
Eseek,
Etell
};
bsambuf();
virtual ~bsambuf();
int is_open();
bsambuf *open(const char *filename,
int mode, const char *keywords = 0,
int willseek = 0,
bsam_exit_list *user_exits = 0);
bsambuf *attach(DCB_t *dcb, int mode,
int willseek = 0);
int init_directory();
int delete_member(const char *name);
int rename_member(const char *old_name,
const char *new_name);
int stow(const char *name,
char action = 'R',
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
int add_member(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
int replace_member(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0);
int find(const char *name);
bsambuf *close();
char dcbrecfm();
short dcblrecl();
short dcbblksize();
DCB_t *getdcb();
int error_info(error_id& id);
void clear_error_info();
void set_user_data(void *user_data);
void *get_user_data();
const char *get_ddname();
const char *get_member();
virtual streambuf *setbuf(char *buffer,
int length);
virtual streampos seekoff(streamoff
offset,ios::seek_dir dir, int mode);
virtual streampos seekpos(streampos
pos, int mode);
virtual int sync();
};<bsamstr.h>
header file defines
class bsambuf
. The
bsambuf
class is a specialization of
class streambuf
that implements
I/O via the record-oriented
BSAM interface of the OS Low-Level I/O functions.
bsambuf
objects are intended for
use in C++ programs that use the SPE version of the library, but they may
also be useful in other contexts.
In addition to the expected
streambuf
functionality,
class bsambuf
permits functions to be called as BSAM exits via objects of class
bsam_exit_list
.
For more information about the OS Low-Level I/O functions, refer to SAS/C Library Reference, Third Edition, Volume 2, Release 6.00. For more information about BSAM, refer to the IBM publication MVS/XA Data Administration Guide (GC26-4140).
bsambuf
.
ios::binary
mode, the record is padded with null
(all bits 0) characters, otherwise the record is padded with blank characters.
Padding characters are not removed from records on input.
setbuf
, whichever is smaller. In no case can characters be pushed
back beyond a record boundary.
class bsambuf
inherits characteristics from
class streambuf
. See the description of this parent class for the details on functions
and operators that are inherited.class bsambuf
defines one constructor:
bsambuf::bsambuf()bsambuf
object for an unopened file.class bsambuf
destructor:
virtual bsambuf::~bsambuf()class bsambuf
defines one type,
enum error_id
. This type enumerates the values that may be retrieved by the
error_info
function, described later in this section.class bsambuf
. The virtual functions are described later in this section.
int bsambuf::is_open()bsambuf
is
connected to an open file; returns 0 otherwise.bsambuf *bsambuf::open
(const char *filename, int
mode,
const char *keywords = 0,
int willseek = 0,
bsam_exit_list *user_exits
= 0)filename
and connects the
bsambuf
to it. If the open is successful,
open
returns a pointer to the
bsambuf
. If an error occurs during
the open,
open
returns 0.
filename
is a DDname optionally followed by a parenthesized
member name.
filename
may be in upper- or lowercase. For example, '
sysin
' is
a valid
filename, as is '
obj(subrtn)
'.
mode
is a combination of one or
more
enum open_mode
flags. One or more of
ios::in, ios::out
, or
ios::app
must
be set.
keywords
is a string of 0 or more DCB macro keywords. The supported
keywords are DSORG, RECFM, LRECL, BLKSIZE, OPTCD, NCP, and BUFNO. The keywords
and their values can be in either upper- or lowercase. If several keywords
are specified, they can be separated by blanks or commas. For example, the
keyword
string '
recfm=fb,blksize=6400,lrecl=80
' corresponds to
the DCB specification DCB=(RECFM=FB,BLKSIZE=6400,LRECL=80). By default, no
keywords are used.
DCB attributes may be specified via the keywords string or by the DD statement or TSO ALLOCATE for the DDname. If any attributes are unspecified for a new file, the following default values are used:
| Format | Default Blocksize |
| RECFM=F | either the logical record length or the largest multiple of the logical record length less than 6144, whichever is larger |
| RECFM=V | either the logical record length + 4 or 6144, whichever is larger |
| RECFM=U | 255 |
If the value of
willseek
is 0, seeking is disabled for this file.
Any attempt to use either the
seekoff
and
seekpos
function
will result in an error. Specify a value other than 0 for this parameter
if you intend to use either
seekoff
or
seekpos
on this file. The default
value is 0.
user_exits
is a pointer to a
bsam_exit_list
object describing
a number of functions to be called as BSAM exit routines. See class bsam_exit_list for information about this
parameter. By default, no user exits are enabled.
bsambuf *bsambuf::attach(DCB_t *dcb,
int mode, int willseek =
0)osdcb
or
osbdcb
function to a
bsambuf
and opens the file. If
the open is successful,
attach
returns a pointer to the
bsambuf
. If an error occurs during the open,
attach
returns 0.
mode
and
willseek
have the same meaning as
they have in the
open
function.int bsambuf::init_directory()bsambuf
.
The value returned is 0 if the function succeeds or is a value other than
0 otherwise.int bsambuf::delete_member
(const char *name)name
from
the file connected to the
bsambuf
.
name
may be specified in either
upper- or lowercase. If
name
is shorter
than eight characters it will be padded on the right with blanks. The value
returned is 0 if the member is successfully deleted or is a value other than
0 otherwise.int bsambuf::rename_member (const char *old_name, const char *new_name)old_name
to
new_name
.
old_name
and
new_name
may be specified in either
upper- or lowercase. If either name is shorter
than eight characters it will be padded on the right with blanks. The value
returned is 0 if the member is successfully renamed or is a value other than
0 otherwise.int bsambuf::stow(const char *name,
char action = 'R',
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0)bsambuf
. The file must be a PDS.
name
is the name of the member or
alias.
name
may be specified in either upper- or lowercase. If
name
is shorter than eight characters, it will be padded on the right
with blanks.
action
may be either 'R', indicating that
name
replaces an existing member
or alias in the PDS, or 'A', which indicates that
name
is
to be added to the PDS. The default value is 'R'.
user_data_length
is the length,
in bytes, of the user data to be associated with name in the PDS directory.
This value must be nonnegative and no greater than 62.
user_data
is a pointer to the user data. The default value of both
user_data_length
and
user_data
is 0.
If
alias
is a value other than 0, then
name
is treated as an alias to be
added or replaced. The default value of
alias
is 0.
TT
and
R
are the relative track and record number, respectively, of
name
. These
values are ignored when
alias
is 0. By default both are 0. (footnote 2)
TTRN
is the number of TTRN fields in
user_data
. This must be a nonnegative
number no greater than 3. By default it is 0.
stow
returns a value other than
0 if any of the arguments are out of bounds. If
stow
invokes the STOW macro, with
one exception,
stow(
) returns the return code from the STOW macro.
(This code can also be retrieved via the
error_info
function.) In general,
the STOW macro returns 0 if the requested action succeeded, or a value other
than 0 otherwise. The exception occurs when
action
is 'R' and the STOW macro returns 8. This return code from
the STOW macro indicates that the member or alias did not previously exist
and so was added. In this case,
stow
returns 0.
int bsambuf::add_member(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0)stow
function when
action
is
'A'. The arguments and return value are identical to
stow
.int bsambuf::replace_member
(const char *name,
int user_data_length = 0,
const void *user_data = 0,
int alias = 0, int TT = 0,
int R = 0, int TTRN = 0)stow
function when
action
is
'R'. The arguments and return value are identical to
stow
.int bsambuf::find(const char *name)name
.
name
may be specified in either
upper- or lowercase. If name is shorter than eight characters it will be padded
on the right with blanks.
find
returns
0 if the file is successfully positioned, or a value other than 0 otherwise.bsambuf *bsambuf::close()bsambuf
from it (even if errors occur). The
bsambuf
's I/O state is cleared. If the close is successful,
close
returns
a pointer to the
bsambuf
. If an error occurs,
close
returns
0.char bsambuf::dcbrecfm()bsambuf
. The
<osio.h>
header file contains preprocessor symbol
definitions for the bits that may be set in this field. The most commonly
used flags are shown in the following table:
| Symbol | Format |
|---|---|
| DCBRECF | fixed |
| DCBRECV | variable |
| DCBRECU | undefined (see Note) |
| DCBRECBR | blocked |
Note: DCBRECU is equivalent to ORing DCBRECF with DCBRECV.
When examining the value returned by
dcbrecfm
, you must always check for DCBRECU before testing the DCBRECF
or DCBRECV flags.![[cautend]](../common/images/cautend.gif)
short bsambuf::dcblrecl()
short bsambuf::dcbblksize()bsambuf
.DCB_t *bsambuf::getdcb()bsambuf
.int bsambuf::error_info(error_id& id)error_id
indicating which routine failed.
error_info
retrieves this information,
which may be used for a detailed error analysis.
The value returned in the
id
argument may have one of the
values shown in the following table. Unless otherwise noted, the failing routine
is a BSAM routine in the OS Low-Level I/O group of functions.
| error_id | Routine |
|---|---|
| Enone | (no error) |
| Estorage | GETMAIN (table note 1) error_info |
| Eabend | BSAM (table note 2) bsambuf |
| Eopen | osopen |
| Eclose | osclose |
| Eget | osget (table note 3) osget |
| Eput | osput |
| Efind | osfind |
| Estow | osstow |
| Eflush | osflush |
| Eseek | osseek |
| Etell | ostell |
TABLE NOTE 1:
This value is set when storage cannot be allocated for a buffer. The code returned by
TABLE NOTE 2:
This value is returned when an "ignorable" ABEND has occurred. An ignorable ABEND does not terminate the program but does prevent any further I/O from being performed on the file connected to the
. The value of the return code is that of the completion code
field in the ABEND exit parameter list.
TABLE NOTE 3:
Although end-of-file causes
to return a value other than zero as a return code, this value is
not stored as an error code.
If, after a low-level routine has failed and its error information stored,
a subsequent low-level routine fails, the error information for that failure
is not stored unless
clear_error_info
has been called.
void bsambuf::clear_error_info()error_id
is
Enone
and the return code is 0.void bsambuf::set_user_data
(void *user_data)user_data
for later retrieval by
get_user_data
. The value may be any value required by the program.
It is ignored by the stream.void *bsambuf::get_user_data()set_user_data
.void *bsambuf::get_ddname()open
. The DDname is uppercased and terminated by a '
\0
'. If the DCB was associated to the
bsambuf
by the
attach
function,
the pointer points to a 0-length string.void *bsambuf::get_member()open
. The member name is uppercased and terminated by a '
\0
'. If the DCB was associated
to the bsambuf by the
attach
function, or the filename did not specify
a member name, the pointer points to a 0-length
string.
virtual streambuf *bsambuf::setbuf
(char *buffer, int length)buffer
and
containing
length
bytes as a buffer for use by the
bsambuf
. If
buffer
is
0 or length is less than or equal
to 0, the
bsambuf
is unbuffered. (However, buffering by BSAM will still take
place.) This function must be called before any I/O is requested for this
bsambuf
and can be called only once for the
bsambuf
.
If the buffer will be used by the
bsambuf
,
setbuf
returns a pointer to the
bsambuf
.
If this function is called after I/O has been requested for the
bsambuf
, it does nothing and returns 0. If this function is called more
than once, subsequent calls for the
bsambuf
do nothing except return
0. This function does not affect the I/O state of the
bsambuf
.
By default, the buffer size is equal to the number of data characters
in a record. In most cases, use of the
setbuf
function will have little,
if any, effect on I/O performance. However, changing the
bsambuf
to
be unbuffered will severely degrade the performance. Also note that the underlying
BSAM routines will always buffer the data,
whether the
bsambuf
is unbuffered or buffered.
virtual streampos bsambuf::seekoff
(streamoff offset, ios::seek_dir
dir, int mode)offset
and
dir
. For a
bsambuf
,
seekoff
will only accept 0 as the
value of
offset
. The only acceptable values for
dir
are
ios::beg
and
ios::cur
.
(Refer to the description of
streambuf::seekoff
for an explanation
of these two values.) If
dir
is
ios::beg
, the file is positioned
to the beginning. If
dir
is
ios::cur
, the position of the file
is not changed. The new location of the
file is returned in the
streampos
.
For
bsambuf
objects, the get and put pointers are the same, so the
mode
argument is ignored.
seekoff
returns
(streampos) EOF
if
offset
is a value other than 0, if
dir
is neither
ios::beg
or
ios::cur
, or if the file position cannot be moved.
seekoff
will
also
always return
(streampos) EOF
if
willseek
was 0 when the file was connected to the
bsambuf
by either
open
or
attach
.
virtual streampos bsambuf::seekpos
(streampos pos, int mode)streampos
. This function returns the new position, or
seekpos(EOF)
if an error occurs. For
bsambuf
objects, the get and put pointers are the
same, so the
mode
argument is ignored.virtual int sync()bsambuf
to be synchronized with the state of the file it is associated
with. If some characters have been buffered for output they will be written
to the file. (footnote 3) Characters that have been buffered for input will be discarded.
Note that synchronization is not possible unless the file is positioned at
a record boundary.
This function returns 0 if synchronization succeeds, otherwise it returns EOF.
class bsam_exit_list, class bsamstream,
class ibsamstream, class obsamstream#include <bsamstr.h>
class bsam_exit_list {
bsam_exit_list();
bsam_exit_list(exit_t *list);
int use(_e_exit code, _e_exit_fp exit);
int use(_e_exit code, void *exit);
int remove(_e_exit code,_e_exit_fp exit);
int remove(_e_exit code, void *exit);
};<bsamstr.h>
header file defines
class bsam_exit_list
. A pointer to a
bsam_exit_list
object can be passed to the
bsambuf::open
,
bsamstream::open
,
ibsamstream::open
, or
obsamstream:open
function to define
one or more functions that are to be used as DCB exit routines.bsam_exit_list
object. OS Low-Level I/O defines only 18 distinct exits.
bsam_exit_list
defines two constructors:
bsam_exit_list::bsam_exit_list()bsam_exit_list::bsam_exit_list
(exit_t list[])list
.
list
is an array of type
exit_t
. This
type is defined as shown here:
typedef struct {
unsigned exit_code;
union {
_e_exit_fp exit_addr;
void *area_addr;
};
} exit_t;
exit_code
may be any one of the values defined for the enumeration
type
_e_exit
, which is also defined as shown here:
enum _e_exit {INACTIVE = 0, INHDR = 1,
OUTHDR = 2, INTLR = 3, OUTTLR = 4,
OPEN = 5, EOV = 6, JFCB = 7,
USER_TOTAL = 10, BLK_COUNT = 11,
DEFER_INTLR = 12,
DEFER_NONSTD_INTLR = 13, FCB = 16,
ABEND = 17, JFCBE = 21,
TAPE_MOUNT = 23, SECURITY = 24,
LAST = 128, SYNAD = 256};
The last entry in the list must set
area_addr
to 0 and have an
exit_code
value of LAST or, if either
exit_addr
or
area_addr
is a value other than 0, you must OR the
exit_code
value with LAST.
Here is an example using this constructor:
static int open_exit(void *r1, void *r0)
{
// DCB open exit processing...
return 0;
}
static int abend_exit(void *r1, void *r0)
{
// DCB abend exit processing...
return 0;
}
int main()
{
exit_t exit_array[] = {{OPEN, open_exit},
{ABEND, abend_exit},
{LAST, 0}
} ;
// Construct "list" with
// two DCB exits.
bsam_exit_list list(exit_array);
// Open DDname "OUT"
// defining two DCB exits.
obsamstream o("out", ios::out, 0, 0,
&list);
/* remainder of program */
.
.
. class bsam_exit_list
destructor:
virtual bsam_exit_list::~bsam_exit_list()class bsam_exit_list
. This class defines no virtual functions.
Note: BSAM exits may be either functions or data areas. Therefore,
class
bsam_exit_list
defines functions for both types.
int use(_e_exit code, _e_exit_fp exit)bsam_exit_list
. The pointer must have type
_e_exit_fp
, which is defined as:
typedef _ _remote int (*_e_exit_fp)
(void *, void *)
The exit must be a static member function or a nonmember function. The
BSAM exit type is specified by
code
. If the exit is added,
use
returns 0. Otherwise it returns a value other than 0.
On entry to a BSAM exit, the first argument is the value of general register 1 as established by BSAM. Likewise, the second argument is the value of general register 0.
bsambuf
. If you define an DCB ABEND exit for the
bsambuf
, your
exit will be run first, followed by the default exit. Your exit must set
the option mask byte in the ABEND exit parameter list before returning. If
the option mask byte is set to 4, indicating that the ABEND should
be "ignored," the default exit causes the DCB to be closed and freed.
For your convenience, the
<bsamstr.h>
header file contains
a partial definition of the parameter list BSAM creates for the DCB ABEND
exit. On entry to the exit function, general register 1 (the first argument)
contains a pointer to this parameter list:
struct Abend_exit_parms {
char completion_code :12;
char :4;
char return_code;
union {
struct {
char :4;
char recover :1;
char ignore :1;
char delay :1;
char :1;
} can;
char option_mask;
};
DCB_t *dcb;
};
// Set "option_mask" to this value
// to ignore the ABEND.
const int IGNORE_ABEND = 4;int remove(_e_exit code, _e_exit_fp exit)bsam_exit_list
. If an exit having the matching exit code and function pointer is not in
the
bsam_exit_list
,
remove
returns a value other than 0. Otherwise,
remove
removes the exit and returns 0.int use(_e_exit code, void *exit)bsam_exit_list
. The BSAM exit type is specified by
code
.
If the exit is added,
use
returns 0. Otherwise it returns a value other
than 0.
int remove(_e_exit code, void *exit)bsam_exit_list
. If an exit having
the matching exit code and address is not in the
bsam_exit_list
,
remove
returns
a value other than 0. Otherwise,
remove
removes the exit and returns
0.class bsambuf, class bsamstream,
class ibsamstream, class obsamstream
#include <fstream.h>
class filebuf : public streambuf
{
public:
filebuf();
virtual ~filebuf();
int is_open();
filebuf* open(const char *name,
int mode,
const char *amparms = "",
const char *am = "");
filebuf* close();
virtual streampos seekoff(streamoff
offset, seek_dir place,
int mode = ios::in|ios::out);
virtual streampos seekpos(streampos pos,
int mode = ios::in|ios::out);
virtual streambuf* setbuf(char *p,
size_t len);
virtual int sync();
};fstream.h
header file defines
class filebuf
.
filebuf
objects represent the lowest level of file I/O
that is standard C++. They provide a specialized form of
streambufs
that uses a file as the source or destination (sink) for characters. Input
corresponds to file reads and output corresponds to file writes. For
filebuf
objects, the get and put pointers are tied together. That is, if
you move one, you move the other. If the file has a format that allows seeks,
a
filebuf
allows seeks.
filebuf
I/O guarantees at least
four characters of putback. You do not need to perform any special action
between reads and writes (in contrast to standard C I/O, where such seeks
are required).
When a
filebuf
is connected to a file, the
filebuf
is said to be open. There
is no default open mode, so you must always specify the open mode when you
create a
filebuf
.
class filebuf
inherits characteristics from
class streambuf
. See the description of this parent class for the details on functions
and operators that are inherited.class filebuf
defines one constructor:
filebuf::filebuf() class filebuf
destructor:
virtual filebuf::~filebuf()class filebuf
. The virtual functions are described later in this section.
int filebuf::is_open()filebuf
is connected to an open
file; returns 0 otherwise.filebuf* filebuf::open(const char *name,
int mode, const char *amparms
= "",
const char *am = "")name
and connects the
filebuf
to
it. If the open is successful,
open()
returns a pointer to the
filebuf
. If an error occurs during the
open (for example, if the file is already open),
open()
returns 0. See enum open_mode for a description of the
mode
argument.
An explanation of filename specification and the arguments
amparms
and
am
can be found in the SAS/C Library Reference, Volume 1.
(Note that the order of the
amparms
and
am
arguments in this function is
the opposite of the order in which they appear in calls to the C
afopen
function.)
You may also want to refer to the SAS/C Compiler and Library User's
Guide.
filebuf* filebuf::close()filebuf
from it (even if errors occur). Also, the
filebuf
's I/O state is cleared. If the close is successful,
close()
returns a pointer to the
filebuf
. If an error occurs during
the close,
close()
returns 0.class streambuf
).
virtual streampos filebuf::seekoff
(streamoff offset, seek_dir
place,
int mode = ios::in|ios::out)place
and
offset
. (Descriptions of
offset
and
place
are contained in the
streambuf::seekoff()
description.)
This function returns the new position, or it returns
streampos(EOF)
if an error occurs (for example, the file may not support seeking, or you
may have requested a seek to a position preceding the beginning of the file).
The position of the file after an error is undefined. Some files support seeking
in full, and some impose lesser
or greater restrictions on seeking.
seekoff()
corresponds to the C
fseek
function, and
seekpos()
corresponds to the C
fsetpos
function.
Rules for these similar C functions
are given in the SAS/C Library Reference, Volume 1, Third Edition, Release
6.00.
For
filebuf
objects, the get and put pointers are the same (moving either
one moves the other). Because of this, you do not have to use the last argument,
mode
.
virtual streampos filebuf::seekpos
(streampos pos,
int mode = ios::in|ios::out)streampos
. This function returns the new position, or it returns
seekpos(EOF)
if an error occurs. For
filebuf
objects, the get and put
pointers are the same (moving either
one moves the other). Because of this, you do not have to use the last argument,
mode
.virtual streambuf* filebuf::setbuf
(char *p, size_t len)p
and containing
len
bytes as a buffer for use by the
filebuf
. If
p
is null
or
len
is less than or equal to 0, the
filebuf
is unbuffered. (However,
buffering by the SAS/C Library and the operating system may still take place.)
This function must be called before any I/O is requested for this
filebuf
and can be called only once for the
filebuf
. Under normal conditions,
setbuf()
returns a pointer to the
filebuf
.
If this function is called after I/O has been requested for the
filebuf
, this function does nothing and returns
NULL
. If this function is called
more than once, subsequent calls for the
filebuf
do nothing except return
NULL
. This function does not affect the I/O state of the
filebuf
.
virtual int filebuf::sync()filebuf
to be synchronized with the state of the file it is associated
with. This attempt at synchronization may result in the following:
filebuf
objects directly access the native I/O
facilities of the system on which they are implemented. For this release of
the SAS/C C++ Development System,
filebuf
objects are implemented
in terms of C
FILE*
s. This may be changed
in later versions of this library, and no assumptions should be made of this
particular implementation.class fstream, class ifstream, class ofstream
#include <stdiostream.h>
class stdiobuf : public streambuf
{
public:
stdiobuf(FILE *file);
virtual ~stdiobuf( );
int is_open( );
FILE* stdiofile( );
streampos seekoff(streamoff offset,
seek_dir place,
int mode =
ios::in|ios::out);
streampos seekpos(streampos pos,
int mode =
ios::in|ios::out);
virtual int sync();
};stdiostream.h
header file declares
class stdiobuf
.
stdiobufs
are intended to be an interface to ANSI C style
FILE
*s on those systems that provide
FILE
*s. Calls to
stdiobuf
member
functions are mapped directly to calls to ANSI C
stdio
functions.
Because
stdiobuf
objects provide no buffering other than that provided by
the C
stdio
functions, any changes to file attributes or contents made
via a
stdiobuf
are reflected immediately in the
stdio
data structures. This includes
file positioning using
seekoff()
or
seekpos()
. For
stdiobuf
objects,
the get and put pointers are tied together. That is, if you move one, you
move the other.
Unless you are mixing
streambuf
and
stdio
access to the same file, you
should use
class filebuf
instead of
class stdiobuf
. Use of
filebuf
objects
may improve performance.
class stdiobuf
inherits characteristics from
class streambuf
. See the description of this parent class for the details on functions
and operators that are inherited.class stdiobuf
defines one constructor:
stdiobuf::stdiobuf(FILE *file)stdiobuf object
associated with an open
FILE
*.class stdiobuf
destructor:
virtual stdiobuf::~stdiobuf()
FILE
*, if opened.class stdiobuf
. The redefined virtual functions are described later
in this section.
int stdiobuf::is_open()stdiobuf
is connected to an open
file; returns 0 otherwise.FILE* stdiofile()FILE
*.class streambuf
).
streampos stdiobuf::seekoff
(streamoff offset, seek_dir
place,
int mode = ios::in|ios::out)streambuf
.
place
can be one of the following:
ios::begios::curios::endoffset
is a positive or negative integer position
relative to place.
mode
can be one of the following:
ios::inios::outios::in|ios::outWhether
seekoff()
and
seekpos()
work for an
stdiobuf
depends on the characteristics
of the associated
FILE*
. See the SAS/C Library Reference, Volume
1, Third
Edition, Release 6.00 for more information on
FILE*
characteristics.
streampos stdiobuf::seekpos
(streampos pos,
int mode = ios::in|ios::out)streambuf
.
pos
must be a value returned by a previous call to
seekoff()
.
mode
can be one of the following:
ios::inios::outios::in|ios::outseekpos()
returns
streampos(EOF)
to indicate an error occurred. See
the documentation for particular stream buffer classes (such as
filebuf
) for
more information on what
kinds of seeking are allowed.virtual int stdiobuf::sync()stdiobuf
to be synchronized with the state of the associated file.
This attempt at synchronization may result in the following:
class stdiostream
#include <iostream.h>
class streambuf
{
public:
int in_avail();
int out_waiting();
int sbumpc();
int sgetc();
int sgetn(char *s, int n);
int snextc();
void stossc();
int sputbackc(char c);
int sputc(int c);
int sputn(const char *s, int n);
virtual int sync();
virtual streampos seekoff(streamoff
offset, seek_dir place,
int mode = ios::in|ios::out);
virtual streampos seekpos(streampos pos,
int mode = ios::in|ios::out);
virtual streambuf* setbuf(char *p,
int len);
};class streambuf
is declared in the
iostream.h
header file and is the base class for all stream buffers. Stream buffers
manage the flow of characters between the program and the ultimate sources
or consumers of characters, such as external files. The
streambuf
class defines behavior common to all stream buffers. More specialized classes
can be derived from
class streambuf
to implement appropriate buffering
strategies for particular stream types. For instance,
filebufs
implement
buffering suitable for file input or output and
strstreambufs
implement buffering
suitable for transfer of data from strings in memory. A
streambuf
is almost never used directly (classes derived from it are used instead) but
more often acts as an interface specification for derived classes.
The functions defined by the
streambuf
interface are divided
into two groups: nonvirtual functions and virtual functions. These sets of
functions are described separately.
class streambuf
defines two constructors and one
destructor. All these functions are protected. This ensures that
class streambuf
is used only as a base class for derived classes.streambuf
interface.
int streambuf::in_avail()streambuf
. Generally, this information is useful only for classes derived
from
class streambuf
.int streambuf::out_waiting()streambuf
but have not been delivered to its ultimate destination.
Generally, this information is useful only for classes derived from
class streambuf
.int streambuf::sbumpc()int streambuf::sgetc()int streambuf::sgetn(char *s, int n)s
and positions the get pointer after the last extracted character. If there
are less than n characters between the get pointer and the end of the stream,
those characters are extracted into
s
and the get pointer is moved to
the end of the stream. This function returns the
number of characters extracted into
s
.int streambuf::snextc()void streambuf::stossc()int streambuf::sputbackc(char c)c
.
c
must be the character that the get pointer is moved over; if it
is not, the effects of this function are undefined. For example, if the get
pointer is at the start of the stream and you call this function, the effect
is undefined.
Also, each class derived from
streambuf
may impose a limit on
the number of characters that can be moved over by
sputbackc()
. If you exceed this
limit, this function returns EOF. For some classes, you cannot back up over
any characters.
int streambuf::sputc(int c)c
in the position following the put pointer, replacing
any pre-existing character, and then advances the put pointer one position.
This function returns
c
if the operation is successful, or EOF if an
error occurs.int streambuf::sputn(const char *s, int n)n
characters
addressed by
s
, replacing any pre-existing characters in those positions, and
then advances the put pointer past the last character stored. This function
returns the number of characters successfully stored.class streambuf
. These functions can be redefined in derived classes (both those supplied
by the library and those you define yourself) to customize the behavior of
streambuf
objects.
virtual int streambuf::sync()streambuf
to be synchronized with the state of the sink or source
it is associated with. This function returns 0 if successful, or EOF if an
error occurs.virtual streampos streambuf::seekoff
(streamoff offset, seek_dir
place,
int mode = ios::in|ios::out)streambuf
.
place
can be one of the following:
ios::begios::curios::endoffset
is a positive or negative integer position
relative to place.
mode
can be one of the following:
ios::inios::outios::in|ios::outstreampos(EOF)
to indicate an error occurred. See
the documentation for particular stream buffer classes (such as
filebuf
) for
more information on what kinds of seeking
are allowed.virtual streampos streambuf::seekpos
(streampos pos,
int mode = ios::in|ios::out)streambuf
.
pos
must be a value returned by a previous call to
seekoff()
.
mode
can be one of the following:
ios::inios::outios::in|ios::outseekpos()
returns
streampos(EOF)
to indicate an error occurred. See
the documentation for particular stream buffer classes (such as
filebuf
) for
more information on what
kinds of seeking are allowed.virtual streambuf* streambuf::setbuf
(char *p, int len)streambuf
.#include <strstream.h>
class strstreambuf : public streambuf
{
public:
strstreambuf( );
strstreambuf(int len);
strstreambuf(void* (*a) (long),
void (*f) (void*));
strstreambuf(char *b, int size,
char *pstart = 0);
~strstreambuf( );
void freeze(int n = 1);
char* str( );
virtual streambuf* setbuf(char *p,
int len);
inc sync( );
virtual streampos seekoff
(streamoff offset,
seek_dir place,
int mode = ios::in|ios::out);
virtual streampos seekpos
(streampos pos,
int mode = ios::in|ios::out);
};strstream.h
declares
class strstreambuf
, which specializes
class streambuf
to provide for I/O using arrays
of
char
(strings).
For
strstreambuf
objects, the get and put pointers are separate. That
is, if you move one of these pointers you do not necessarily move the other.
strstreambuf
objects are created in one of two different modes, dynamic
mode or static (fixed) mode. Once a
strstreambuf
is created, it does
not change modes. The following list explains the difference between fixed
and dynamic mode:
strstreambuf
does not have a fixed size and grows
as needed. When the array associated with a dynamic mode
strstreambuf
is filled, the
strstreambuf
automatically allocates a larger array,
copies the old smaller array to the larger,
and frees the smaller array. The functions used to handle allocating and freeing
the arrays are determined by the constructor used to create the
strstreambuf
(see the description of constructors for class strstreambuf .strstreambuf
has a fixed size that does not change.
If the array associated with a static mode
strstreambuf
is filled, further
writes to the
strstreambuf
may corrupt memory. Be cautious when inserting into
static mode
strstreambuf
s.Note: Do not confuse static mode with the
static
storage class modifier.
class strstreambuf
defines some member functions of its own and also
redefines several virtual functions from the base class.
class strstreambuf
inherits characteristics from
class streambuf
. See the description of this parent class for the
details on functions and operators that are inherited.class strstreambuf
defines four constructors:
strstreambuf::strstreambuf()strstreambuf::strstreambuf(int len)strstreambuf::strstreambuf
(void*(*a)(long), void (*f)(void*))strstreambuf
object in dynamic mode.
a
is the allocation function to be used to do the dynamic allocation
and takes as its argument a
long
, which specifies the number of bytes to allocate.
If
a
is
NULL
, the
operator new
is used instead of
a
.
f
is the deallocation function, which frees the space allocated by
a
.
f
takes as its argument a pointer to an array allocated by
a
. If
f
is
NULL
, the
operator delete
is used instead of
f
.strstreambuf::strstreambuf(char *b,
int size, char *pstart =
0)strstreambuf
object in static mode;
it does not grow dynamically.
b
specifies where to start the array
and
size
specifies the size of the array, as explained in the following
list.
size
is positive, the array is
size
bytes
long.
size
is 0, the function assumes
b
points to
the start of a null-terminated string. In this case, the string, not including
the '
\0
' character, is considered to be the
strstreambuf
.
size
is negative, the
strstreambuf
is assumed to be indefinitely
long.
b
and the
put pointer receives the value of
pstart
. If
pstart
is
NULL
, then storing characters in the
strstreambuf
is not allowed and
causes the function to return an error.
class strstreambuf
destructor:
strstreambuf::~strstreambuf()strstreambuf
object. The destructor causes any
memory allocated for the
strstreambuf
to be freed.class strstreambuf
defines two nonvirtual member
functions.
void strstreambuf::freeze(int n = 1)n
is nonzero, which is the default, the array is not deleted automatically.
If n is 0, the array is unfrozen and is deleted automatically. The array is
deleted whenever a dynamically created
strstreambuf
needs more space or
when the destructor is called. This function only applies
to dynamically created
strstreambuf
s; it has no effect on statically created
strstreambuf
s.
If you try to store characters in a frozen array, the effect is undefined.
char* strstreambuf::str()str()
has been called, the effect
of storing characters in the array is undefined until the strstreambuf is
unfrozen by calling
freeze(0)
.class strstreambuf
redefines several virtual functions
from its base class (
class streambuf
).
virtual strstreambuf::streambuf* setbuf
(char *p, int len)strstreambuf
that the next time an array is dynamically
allocated it should be at least
len
bytes long.
p
is ignored.int strstreambuf::sync()virtual streampos strstreambuf::seekoff
(streamoff offset, seek_dir
place,
int mode = ios::in|ios::out)strstreambuf
. See the description of
streambuf::seekoff
for explanations of
offset
,
place
and
mode
.
If the
strstreambuf
is in dynamic mode, this function returns
streampos(EOF)
to indicate an error occurred.
If either the get or put pointer is moved to a position outside the
strstreambuf
, or if the put pointer is moved for a
strstreambuf
that does not allow output, then
streampos(EOF)
is returned and the
pointers are not moved.
virtual streampos strstreambuf::seekpos
(streampos pos,
int mode = *ios::in|ios::out)strstreambuf
.
If the
strstreambuf
is in dynamic mode, this function returns
streampos(EOF)
to indicate an error occurred.
pos
must be a value returned by
a previous call to
seekoff()
.
See the description of
streambuf::seekpos()
for an explanation of
mode
. If
ios::out
is specified for
mode
and output is not allowed for
this
strstreambuf
, then
streampos(EOF)
is returned to indicate an error
occurred and the put pointer is not moved.
class strstream
This
section describes contents of the
iomanip.h
header file, which provides
predefined manipulators, as well as support functions and classes that enable
you to create your own manipulators.
#include <IOMANIP.h>
/* Macros for creating class names */
#define SMANIP(T) ...
#define SAPP(T) ...
#define IMANIP(T) ...
#define IAPP(T) ...
#define OMANIP(T) ...
#define OAPP(T) ...
#define IOMANIP(T) ...
#define IOAPP(T) ...
// Start of IOMANIPdeclare macro
#define IOMANIPdeclare(T)
class SMANIP(T)
{
public:
SMANIP(T)(ios&(*f)(ios&, T), T d);
friend istream& operator >>(istream& i,
SMANIP(T)& m);
friend ostream& operator <<(ostream& o,
SMANIP(T)& m);
};
class SAPP(T)
{
public:
SAPP(T)(ios&(*f)(ios&, T));
SMANIP(T)operator()(T d);
};
class IMANIP(T)
{
public:
IMANIP(T)(ios&(*f)(ios&, T), T d);
friend istream& operator >>(istream& i,
IMANIP(T)& m);
};
class IAPP(T)
{
public:
IAPP(T)(ios&(*f)(ios&, T));
IMANIP(T)operator()(T d);
};
class OMANIP(T)
{
public:
OMANIP(T)(ios&(*f)(ios&, T), T d);
friend ostream& operator <<(ostream& o,
OMANIP(T)& m);
};
class OAPP(T)
{
public:
OAPP(T)(ios&(*f)(ios&, T));
OMANIP(T)operator()(T d);
};
class IOMANIP(T)
{
public:
IOMANIP(T)(ios&(*f)(ios&, T), T d);
friend istream& operator >>(istream& i,
IOMANIP(T)&m);
friend ostream& operator <<(ostream& o,
IOMANIP(T)&m);
};
class IOAPP(T)
{
public:
IOAPP(T)(ios&(*f)(ios&, T));
IOMANIP(T)operator()(T d);
};
// End of IOMANIPdeclare macro
IOMANIPdeclare(int);
IOMANIPdeclare(long);
SMANIP(int) setw(int width);
SMANIP(int) setbase(int base);
SMANIP(int) setfill(int fill_char);
SMANIP(int) setprecision(int precision);
SMANIP(long) setiosflags(long flags);
SMANIP(long) resetiosflags(long flags);IOMANIP.h
header file declares some predefined
manipulators, as well as support functions and classes that enable you to
create your own manipulators. A manipulator is a value that can
be used to effect some change to a stream by inserting it into or extracting
it from the stream. For example, the
flush
function
is a manipulator of
ostream
objects:
In fact, any function of one the following types is a manipulator:cout << flush; // Causes cout to be flushed.
ostream& (ostream&)ostream
objects.istream& (istream&)istream
objects.ios& (ios&)istream
or
ostream
objects. You can also create manipulators that have arguments. The
IOMANIP.h
header file defines two manipulator-creation classes for each type of stream
(
ios
,
istream
,
ostream
, and
iostream
). One class has a name in the form
xMANIP(T)
, and the other class has a name in the form
xAPP(T)
, where
T
is an identifier that names a type (such as a
typedef
name for a class name) and x is a letter such
as S.
For
ios
objects, these two classes are named
SMANIP(T)
and
SAPP(T)
.
iomanip.h
allow you to control various pieces of the format state of a stream. These
manipulators are described in the following list.
SMANIP(int) setw(int w)SMANIP(int)
) that can be used to
set the
width()
value of an
ios
object.SMANIP(int) setbase(int base)ios
object. The argument must be one of the values 8, 10, or 16.SMANIP(int) setfill(int f)fill()
value of an
ios
object.SMANIP(int) setprecision(int p)precision()
value of an
ios
object.SMANIP(long) setiosflags(long flags)flags()
value of an
ios
object.SMANIP(long) resetiosflags(long flags)flags()
value of an
ios
object.********27,00048
:
cout << setw(10) << setfill('*')
<< 27 << ',' << setw(5)
<< setfill('0') << 48;
The following example transmits
32,5
:
cout << setprecision(2) << 32.1 << ','
<< setprecision(0) << 5.3;
The following example sets the
skipws
bit in
cout
's format
state:
cout << setiosflags(ios::skipws);
The following example clears the
skipws
bit in
cout
's format
state:
cout << resetiosflags(ios::skipws);
IOMANIP.h
header file also provides the means for
you to create your own manipulators. It does this by defining a macro,
IOMANIPdeclare(T)
, that when invoked with a
typedef
name for
T
declares
the following classes:
SMANIP(T) and SAPP(T)ios
objects.IMANIP(T) and IAPP(T) istream
objects.OMANIP(T) and OAPP(T)ostream
objects. IOMANIP(T) and IOAPP(T)iostream
objects.class SMANIP(T)
and
SAPP(T)
are explained in detail in this section;
the other classes are very similar to
SAPP(T)
and only the differences
between them and
class SMANIP
and
SAPP(T)
are noted.
If you are going to create new manipulators using the various
xMANIP(T)
and
xAPP(T)
classes, the classes must first be defined for a particular type name. This
is done by putting the following definition in any module that uses the
xMANIP(T)
or
xAPP(T)
classes for a particular type name:
IOMANIPdeclare(type-name);
where type-name can be any valid type identifier. Because
int
and
long
are the most commonly used type names in manipulators,
the
IOMANIPdeclares
for these type names are included in the
IOMANIP.h
header file, and your program should not declare them again. If
you need to create manipulators using the
xMANIP(T)
and
xAPP(T)
classes for type names other than
int
or
long
, you must include a use of
IOMANIPdeclare()
in your module.
For example, before using
xMANIP(T)
and
xAPP(T)
classes to create manipulators that accept
char
arguments, the
xMANIP(T)
and
xAPP(T)
classes for the type name
char
must
be declared as follows:
If you need to create manipulators that accept arguments of more complicated types, like#include <IOMANIP.h> IOMANIPdeclare(char);
char*
arguments, you must first
declare a
typedef
for the type because
IOMANIPdeclare
requires a single-word
type name. Here is an example:
#include <IOMANIP.h> typedef char* STRING; IOMANIPdeclare(STRING);
SMANIP(T)(ios&(*f)(ios&, T), T d)SMANIP(T)
and returns a single argument
manipulator by collecting the function
f
and argument
d
into a single
manipulator value. It is assumed that
f
is a function that changes
ios
in some way using the value of
d
.friend istream& operator >> (istream& i,
SMANIP(T)& m)
friend ostream& operator << (ostream& o,
SMANIP(T)& m)SMANIP(T)
objects to be "inserted-into"
istream
objects and "extracted-from"
ostream
objects, respectively. They
each use the values
f
and
d
from the
SMANIP(T)
value. They then call
f(myios,d)
where
myios
is the
ios
part of
i
or
o
, respectively. It is assumed that
f
is a function that changes
ios
in some way using the value of
d
.SAPP(T)
, than to use the
xMANIP(T)
classes.
class SAPP(T)
is described next.SAPP(T)
objects make it easier to use
SMANIP(T)
objects. The EXAMPLES
section gives an example of using an
SAPP(T)
object. Here are the members
of
class SAPP(T)
:
SAPP(T) (ios&(*f)(ios&, T))SAPP(T)
object to contain
f
.SMANIP(T) operator() (T d)SMANIP(T)
object using the
f
from the
SAPP(T)
and the
d
argument.IOMANIPdeclare(T)
are similar to class
SAPP(T)
, with the following differences:
setwidth
,
which works like the library's
setw
.
ios& setw_func(ios& i, int w)
{
i.width(w);
return i;
}
SAPP(int) setwidth(setw_func);
FOOTNOTE 2:
class bsambuf defines no function that can retrieve this
information. Use the
osnote
function in the OS Low-Level I/O group of
functions to get the TTRz value for the current position in the file.
FOOTNOTE 3:The
sync
function invokes the low-level
osflush
routine to flush all buffered characters to the operating
system. Note, however, that all characters may not be written immediately,
due to additional buffering performed by the operating system.
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © Tue Feb 10 12:11:23 EST 1998 by SAS Institute Inc., Cary, NC, USA. All rights reserved.