-1- This clause describes components that C++ programs may use to perform input/output operations.
-2- The following subclauses describe requirements for stream parameters, and components for forward declarations of iostreams, predefined iostreams objects, base iostreams classes, stream buffering, stream formatting and manipulators, string streams, and file streams, as summarized in Table ??:
Subclause | Header(s) |
lib.iostreams.requirements Requirements | |
lib.iostream.forward Forward declarations | <iosfwd> |
lib.iostream.objects Standard iostream objects | <iostream> |
lib.iostreams.base Iostreams base classes | <ios> |
lib.stream.buffers Stream buffers | <streambuf> |
lib.iostream.format Formatting and manipulators | <istream> |
<ostream> | |
<iomanip> | |
lib.string.streams String streams | <sstream> |
<cstdlib> | |
lib.file.streams File streams | <fstream> |
<cstdio> | |
<cwchar> |
27.1 - Iostreams requirements [lib.iostreams.requirements]
27.1.1 - Imbue Limitations [lib.iostream.limits.imbue]
No function described in clause lib.input.output except for
ios_base::imbue
causes any instance of
basic_ios::imbue
or
basic_streambuf::imbue
to be called.
If any user function called from a function declared in clause lib.input.output or
as an overriding virtual function of any class declared in clause lib.input.output
calls
imbue,
the behavior is undefined.
27.1.2 - Positioning Type Limitations [lib.iostreams.limits.pos]
The classes of clause lib.input.output with template arguments
charT
and
traits
behave as described if
traits::pos_type
and
traits::off_type
are
streampos
and
streamoff
respectively.
Except as noted explicitly below, their behavior when
traits::pos_type
and
traits::off_type
are other types is implementation-defined.
27.2 - Forward declarations [lib.iostream.forward]
Header <iosfwd> synopsis
namespace std { template<class charT> class char_traits; template<> class char_traits<char>; template<> class char_traits<wchar_t>;
template<class T> class allocator;
template <class charT, class traits = char_traits<charT> > class basic_ios; template <class charT, class traits = char_traits<charT> > class basic_streambuf; template <class charT, class traits = char_traits<charT> > class basic_istream; template <class charT, class traits = char_traits<charT> > class basic_ostream; template <class charT, class traits = char_traits<charT> > class basic_iostream;
template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > class basic_stringbuf; template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > class basic_istringstream; template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > class basic_ostringstream; template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > class basic_stringstream; template <class charT, class traits = char_traits<charT> > class basic_filebuf; template <class charT, class traits = char_traits<charT> > class basic_ifstream; template <class charT, class traits = char_traits<charT> > class basic_ofstream; template <class charT, class traits = char_traits<charT> > class basic_fstream;
template <class charT, class traits = char_traits<charT> > class istreambuf_iterator; template <class charT, class traits = char_traits<charT> > class ostreambuf_iterator;
typedef basic_ios<char> ios; typedef basic_ios<wchar_t> wios;
typedef basic_streambuf<char> streambuf; typedef basic_istream<char> istream; typedef basic_ostream<char> ostream; typedef basic_iostream<char> iostream;
typedef basic_stringbuf<char> stringbuf; typedef basic_istringstream<char> istringstream; typedef basic_ostringstream<char> ostringstream; typedef basic_stringstream<char> stringstream;
typedef basic_filebuf<char> filebuf; typedef basic_ifstream<char> ifstream; typedef basic_ofstream<char> ofstream; typedef basic_fstream<char> fstream;
typedef basic_streambuf<wchar_t> wstreambuf; typedef basic_istream<wchar_t> wistream; typedef basic_ostream<wchar_t> wostream; typedef basic_iostream<wchar_t> wiostream;
typedef basic_stringbuf<wchar_t> wstringbuf; typedef basic_istringstream<wchar_t> wistringstream; typedef basic_ostringstream<wchar_t> wostringstream; typedef basic_stringstream<wchar_t> wstringstream;
typedef basic_filebuf<wchar_t> wfilebuf; typedef basic_ifstream<wchar_t> wifstream; typedef basic_ofstream<wchar_t> wofstream; typedef basic_fstream<wchar_t> wfstream;
template <class state> class fpos; typedef fpos<char_traits<char>::state_type> streampos; typedef fpos<char_traits<wchar_t>::state_type> wstreampos;
}
-1- Default template arguments are described as appearing both in <iosfwd> and in the synopsis of other headers but it is well-formed to include both <iosfwd> and one or more of the other headers.*
[Footnote: It is the implementation's responsibility to implement headers so that including <iosfwd> and other headers does not violate the rules about multiple occurences of default arguments. --- end foonote]
-2- [Note: The template class basic_ios<charT,traits> serves as a virtual base class for the classes basic_istream<charT,traits>, basic_ostream<charT,traits>, and classes derived from them. basic_iostream<charT,traits> is a class derived from both basic_istream and basic_ostream.
-3- The template class basic_streambuf<charT,traits> serves as a base class for template classes basic_stringbuf and basic_filebuf.
-4- The template class basic_istream serves as a base class for template classes basic_istringstream and basic_ifstream
-5- The template class basic_ostream serves as a base class for template classes basic_ostringstream and basic_ofstream
-6- The template class basic_iostream serves as a base class for template classes basic_stringstream and basic_fstream.
-7- Other typedefs define instances of template classes specialized for char or wchar_t types.
-8- The template class fpos<charT> is a class used for specifying file position information.
-9- The types streampos and wstreampos are used for positioning streams specialized on char and wchar_t respectively.
-10- This synopsis suggests a circularity between streampos and char_traits<char>. An implementation can avoid this circularity by substituting equivalent types. One way to do this might be
template<class stateT> class fpos { ... }; //depends on nothing typedef ... _STATE; // implementation private declaration of stateT typedef fpos<_STATE> streampos; template<> struct char_traits<char> { typedef streampos pos_type; // ... }
Header <iostream> synopsis
namespace std { extern istream cin; extern ostream cout; extern ostream cerr; extern ostream clog; extern wistream wcin; extern wostream wcout; extern wostream wcerr; extern wostream wclog; }
-1- The header <iostream> declares objects that associate objects with the standard C streams provided for by the functions declared in <cstdio> (lib.c.files).
-2- Mixing operations on corresponding wide- and narrow-character streams follows the same semantics as mixing such operations on FILEs, as specified in Amendment 1 of the ISO C standard. The objects are constructed, and the associations are established at some time prior to or during first time an object of class basic_ios<charT,traits>::Init is constructed, and in any case before the body of main begins execution.*
[Footnote: If it is possible for them to do so, implementations are encouraged to initialize the objects earlier than required. --- end foonote]The objects are not destroyed during program execution.*
[Footnote: Constructors and destructors for static objects can access these objects to read input from stdin or write output to stdout or stderr. --- end foonote]
istream cin;
-1- The object cin controls input from a stream buffer associated with the object stdin, declared in <cstdio>.
-2-
After the object
cin
is initialized,
cin.tie()
returns
&cout.
Its state is otherwise the same as required for
ios_base::init
(lib.basic.ios.cons).
ostream cout;
-3-
The object
cout
controls output to a stream buffer
associated with the object
stdout,
declared in
<cstdio>
(lib.c.files).
ostream cerr;
-4- The object cerr controls output to a stream buffer associated with the object stderr, declared in <cstdio> (lib.c.files).
-5-
After the object
cerr
is initialized,
cerr.flags() & unitbuf
is nonzero.
Its state is otherwise the same as required for
ios_base::init
(lib.basic.ios.cons).
ostream clog;
-6-
The object
clog
controls output to a stream buffer
associated with the object
stderr,
declared in
<cstdio>
(lib.c.files).
27.3.2 - Wide stream objects [lib.wide.stream.objects]
wistream wcin;
-1- The object wcin controls input from a stream buffer associated with the object stdin, declared in <cstdio>.
-2-
After the object
wcin
is initialized,
wcin.tie()
returns
&wcout.
Its state is otherwise the same as required for
ios_base::init
(lib.basic.ios.cons).
wostream wcout;
-3-
The object
wcout
controls output to a stream buffer
associated with the object
stdout,
declared in
<cstdio>
(lib.c.files).
wostream wcerr;
-4- The object wcerr controls output to a stream buffer associated with the object stderr, declared in <cstdio> (lib.c.files).
-5-
After the object
wcerr
is initialized,
wcerr.flags() & unitbuf
is nonzero.
Its state is otherwise the same as required for
ios_base::init
(lib.basic.ios.cons).
wostream wclog;
-6-
The object
wclog
controls output to a stream buffer
associated with the object
stderr,
declared in
<cstdio>
(lib.c.files).
27.4 - Iostreams base classes [lib.iostreams.base]
Header <ios> synopsis
#include <iosfwd> namespace std { typedef OFF_T streamoff; typedef SZ_T streamsize; template <class stateT> class fpos; class ios_base; template <class charT, class traits = char_traits<charT> > class basic_ios;
//lib.std.ios.manip, manipulators: ios_base& boolalpha (ios_base& str); ios_base& noboolalpha(ios_base& str);
ios_base& showbase (ios_base& str); ios_base& noshowbase (ios_base& str);
ios_base& showpoint (ios_base& str); ios_base& noshowpoint(ios_base& str);
ios_base& showpos (ios_base& str); ios_base& noshowpos (ios_base& str);
ios_base& skipws (ios_base& str); ios_base& noskipws (ios_base& str);
ios_base& uppercase (ios_base& str); ios_base& nouppercase(ios_base& str);
//lib.adjustfield.manip adjustfield: ios_base& internal (ios_base& str); ios_base& left (ios_base& str); ios_base& right (ios_base& str);
//lib.basefield.manip basefield: ios_base& dec (ios_base& str); ios_base& hex (ios_base& str); ios_base& oct (ios_base& str);
//lib.floatfield.manip floatfield: ios_base& fixed (ios_base& str); ios_base& scientific (ios_base& str); }
typedef OFF_T streamoff;
-1-
The type
streamoff
is an implementation-defined type
that satisfies the requirements of
lib.fpos.operations.
typedef SZ_T streamsize;
-2- The type streamsize is a synonym for one of the signed basic integral types. It is used to represent the number of characters transferred in an I/O operation, or the size of I/O buffers.*
[Footnote: streamsize is used in most places where ISO C would use size_t. Most of the uses of streamsize could use size_t, except for the strstreambuf constructors, which require negative values. It should probably be the signed type corresponding to size_t (which is what Posix.2 calls ssize_t). --- end foonote]
namespace std { class ios_base { public: class failure;
typedef T1 fmtflags; static const fmtflags boolalpha; static const fmtflags dec; static const fmtflags fixed; static const fmtflags hex; static const fmtflags internal; static const fmtflags left; static const fmtflags oct; static const fmtflags right; static const fmtflags scientific; static const fmtflags showbase; static const fmtflags showpoint; static const fmtflags showpos; static const fmtflags skipws; static const fmtflags unitbuf; static const fmtflags uppercase; static const fmtflags adjustfield; static const fmtflags basefield; static const fmtflags floatfield;
typedef T2 iostate; static const iostate badbit; static const iostate eofbit; static const iostate failbit; static const iostate goodbit;
typedef T3 openmode; static const openmode app; static const openmode ate; static const openmode binary; static const openmode in; static const openmode out; static const openmode trunc;
typedef T4 seekdir; static const seekdir beg; static const seekdir cur; static const seekdir end;
class Init;
//lib.fmtflags.state fmtflags state: fmtflags flags() const; fmtflags flags(fmtflags fmtfl); fmtflags setf(fmtflags fmtfl); fmtflags setf(fmtflags fmtfl, fmtflags mask); void unsetf(fmtflags mask);
streamsize precision() const; streamsize precision(streamsize prec); streamsize width() const; streamsize width(streamsize wide);
//lib.ios.base.locales locales: locale imbue(const locale& loc); locale getloc() const;
//lib.ios.base.storage storage: static int xalloc(); long& iword(int index); void*& pword(int index);
//destructor virtual ~ios_base();
//lib.ios.base.callback callbacks; enum event { erase_event, imbue_event, copyfmt_event }; typedef void (*event_callback)(event, ios_base&, int index); void register_callback(event_call_back fn, int index);
static bool sync_with_stdio(bool sync = true);
protected: ios_base();
private: // static int index; exposition only // long* iarray; exposition only // void** parray; exposition only }; }
-1- ios_base defines several member types:
-2- It maintains several kinds of data:
-3- [Note: For the sake of exposition, the maintained data is presented here as:
namespace std { class ios_base::failure : public exception { public: explicit failure(const string& msg); virtual ~failure(); virtual const char* what() const throw(); }; }
-1-
The class
failure
defines the base class
for the types of all objects thrown as exceptions,
by functions in the iostreams library,
to report errors detected during stream buffer operations.
explicit failure(const string& msg);
-2- Effects: Constructs an object of class failure, initializing the base class with exception( msg ).
-3- Postcondition:
strcmp(what(), msg.c_str()) == 0
const char* what() const;
-4- Returns:
The message msg with which the exception was created.
27.4.2.1.2 - Type ios_base::fmtflags [lib.ios::fmtflags]
typedef T1 fmtflags;
-1- The type fmtflags is a bitmask type (lib.bitmask.types). Setting its elements has the effects indicated in Table ??:
Element | Effect(s) if set |
boolalpha | insert and extract bool type in alphabetic format |
dec | converts integer input or generates integer output in decimal base |
fixed | generate floating-point output in fixed-point notation; |
hex | converts integer input or generates integer output in hexadecimal base; |
internal | adds fill characters at a designated internal point in certain generated output, or identical to right if no such point is designated; |
left | adds fill characters on the right (final positions) of certain generated output; |
oct | converts integer input or generates integer output in octal base; |
right | adds fill characters on the left (initial positions) of certain generated output; |
scientific | generates floating-point output in scientific notation; |
showbase | generates a prefix indicating the numeric base of generated integer output; |
showpoint | generates a decimal-point character unconditionally in generated floating-point output; |
showpos | generates a + sign in non-negative generated numeric output; |
skipws | skips leading white space before certain input operations; |
unitbuf | flushes output after each output operation; |
uppercase | replaces certain lowercase letters with their uppercase equivalents in generated output. |
-2- Type fmtflags also defines the constants indicated in Table ??:
Constant | Allowable values |
adjustfield | left | right | internal |
basefield | dec | oct | hex |
floatfield | scientific | fixed |
27.4.2.1.3 - Type ios_base::iostate [lib.ios::iostate]
typedef T2 iostate;
-1- The type iostate is a bitmask type (lib.bitmask.types) that contains the elements indicated in Table ??:
Element | Effect(s) if set |
badbit | indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error from a file); |
eofbit | indicates that an input operation reached the end of an input sequence; |
failbit | indicates that an input operation failed to read the expected characters, or that an output operation failed to generate the desired characters. |
-2- Type iostate also defines the constant:
typedef T3 openmode;
-1- The type openmode is a bitmask type (lib.bitmask.types). It contains the elements indicated in Table ??:
Element | Effect(s) if set |
app | seek to end before each write |
ate | open and seek to end immediately after opening |
binary | perform input and output in binary mode (as opposed to text mode) |
in | open for input |
out | open for output |
trunc | truncate an existing stream when opening |
27.4.2.1.5 - Type ios_base::seekdir [lib.ios::seekdir]
typedef T4 seekdir;
-1- The type seekdir is an enumerated type (lib.enumerated.types) that contains the elements indicated in Table ??:
Element | Meaning |
beg | request a seek (for subsequent input or output) relative to the beginning of the stream |
cur | request a seek relative to the current position within the sequence |
end | request a seek relative to the current end of the sequence |
27.4.2.1.6 - Class ios_base::Init [lib.ios::Init]
namespace std {
class ios_base::Init {
public:
Init();
~Init();
private:
// static int init_cnt; exposition only
};
}
-1- The class Init describes an object whose construction ensures the construction of the eight objects declared in <iostream> (lib.iostream.objects) that associate file stream buffers with the standard C streams provided for by the functions declared in <cstdio> (lib.c.files).
-2- For the sake of exposition, the maintained data is presented here as:
Init();
-3- Effects:
Constructs an object of class
Init.
If init_cnt is zero,
the function stores the value one in init_cnt ,
then constructs and initializes the objects
cin,
cout,
cerr,
clog
(lib.narrow.stream.objects),
wcin,
wcout,
wcerr,
and
wclog
(lib.wide.stream.objects).
In any case, the function then adds one to the value stored in
init_cnt .
~Init();
-4- Effects:
Destroys an object of class
Init.
The function subtracts one from the value stored in init_cnt
and,
if the resulting stored value is one, calls
cout.flush(),
cerr.flush(),
clog.flush(),
wcout.flush(),
wcerr.flush(),
wclog.flush().
27.4.2.2 - ios_base fmtflags state functions [lib.fmtflags.state]
fmtflags flags() const;
-1- Returns:
The format control information for both input and output.
fmtflags flags(fmtflags fmtfl);
-2- Postcondition: fmtfl == flags().
-3- Returns:
The previous value of
flags().
fmtflags setf(fmtflags fmtfl);
-4- Effects: Sets fmtfl in flags().
-5- Returns:
The previous value of
flags().
fmtflags setf(fmtflags fmtfl, fmtflags mask);
-6- Effects: Clears mask in flags(), sets fmtfl & mask in flags().
-7- Returns:
The previous value of
flags().
void unsetf(fmtflags mask);
-8- Effects:
Clears mask in
flags().
streamsize precision() const;
-9- Returns:
The precision (number of digits after the decimal point) to generate on
certain output conversions.
streamsize precision(streamsize prec);
-10- Postcondition: prec == precision().
-11- Returns:
The previous value of
precision().
streamsize width() const;
-12- Returns:
The minimum field width (number of characters) to generate on certain output
conversions.
streamsize width(streamsize wide);
-13- Postcondition: wide == width().
-14- Returns:
The previous value of
width().
27.4.2.3 - ios_base locale functions [lib.ios.base.locales]
locale imbue(const locale loc);
-1- Effects: Calls each registered callback pair (fn,index) (lib.ios.base.callback) as (*fn)(imbue_event,*this,index) at such a time that a call to ios_base::getloc() from within fn returns the new locale value loc.
-2- Returns: If no locale has been imbued, a copy of the global C++ locale, locale(), in effect at the time of construction. Otherwise, returns the imbued locale, to be used to perform locale-dependent input and output operations
-3- Postcondition:
loc == getloc().
locale getloc() const;
-4- Returns:
The previous value of
getloc().
27.4.2.4 - ios_base static members [lib.ios.members.static]
bool sync_with_stdio(bool sync = true);
-1- Returns: true if the standard iostream objects (lib.iostream.objects) are synchronized and otherwise returns false. The first time it is called, the function returns true.
-2- Effects:
If any input or output operation has occurred using the standard streams prior to the
call, the effect is implementation-defined.
Otherwise, called with a false argument, it allows the standard streams to
operate independently of the standard C streams.
27.4.2.5 - ios_base storage functions [lib.ios.base.storage]
static int xalloc();
-1- Returns:
index
++.
long& iword(int idx);
-2- Effects: If iarray is a null pointer, allocates an array of long of unspecified size and stores a pointer to its first element in iarray . The function then extends the array pointed at by iarray as necessary to include the element iarray[idx]. Each newly allocated element of the array is initialized to zero. The reference returned may become invalid after another call to the object's iword member with a different index, after a call to its copyfmt member, or when the object is destroyed.*
[Footnote: An implementation is free to implement both the integer array pointed at by iarray and the pointer array pointed at by parray as sparse data structures, possibly with a one-element cache for each. --- end foonote]If the function fails*
[Footnote: for example, because it cannot allocate space. --- end foonote]it sets badbit, which may throw an exception.
-3- Returns:
On success
iarray[idx].
On failure, a valid
long&
initialized to 0.
void* & pword(int idx);
-4- Effects: If parray is a null pointer, allocates an array of pointers to void of unspecified size and stores a pointer to its first element in parray . The function then extends the array pointed at by parray as necessary to include the element parray[idx]. Each newly allocated element of the array is initialized to a null pointer. The reference returned may become invalid after another call to the object's pword member with a different index, after call to its copyfmt member, or when the object is destroyed. If the function fails*
[Footnote: for example, because it cannot allocate space. --- end foonote]it sets badbit, which may throw an exception.
-5- Returns: On success parray[idx]. On failure a valid void*& initialized to 0.
-6- Notes:
After a subsequent call to
pword(int)
for the same object, the earlier return value may no longer be valid.
27.4.2.6 - ios_base callbacks [lib.ios.base.callback]
void register_callback(event_callback fn, int index);
-1- Effects: Registers the pair (fn,index) such that during calls to imbue() (lib.ios.base.locales), copyfmt(), or ~ios_base() (lib.ios.base.cons), the function fn is called with argument index. Functions registered are called when an event occurs, in opposite order of registration. Functions registered while a callback function is active are not called until the next event.
-2- Requires: The function fn shall not throw exceptions.
-3- Notes:
Identical pairs are not merged.
A function registered twice will be called twice.
27.4.2.7 - ios_base constructors/destructors [lib.ios.base.cons]
ios_base();
-1- Effects:
Each
ios_base
member has an indeterminate value after construction.
~ios_base()
-2- Effects:
Destroys an object of class
ios_base.
Calls each registered callback pair
(fn,index)
(lib.ios.base.callback) as
(*fn)(erase_event,*this,index)
at such time that any
ios_base
member function called from within
fn
has well defined results.
27.4.3 - Template class fpos [lib.fpos]
namespace std { template <class stateT> class fpos { public: //lib.fpos.members Members stateT state() const; void state(stateT); private; stateT st; // exposition only }; }
void state(stateT s);
-1- Effects:
Assign s to st.
stateT state();
-2- Returns:
Current value of st.
27.4.3.2 - fpos requirements [lib.fpos.operations]
-1- Operations specified in Table ?? are permitted. In that table,
expression | return type | operational | assertion/note |
semantics | pre/post-condition | ||
P(i) | p == P(i) | ||
note: a destructor is assumed. | |||
P p(i); | |||
P p = i; | post: p == P(i) . | ||
P(o) | fpos | converts from offset | |
O(p) | OFF_T | converts to offset | P(O(p)) == p |
p == q | convertible to bool | == is an equivalence relation | |
p != q | convertible to bool | !(p==q) | |
q = p + o | fpos | + offset | q-o == p |
p += o | |||
q = p - o | fpos | - offset | q+o == p |
p -= o | |||
o = p - q | OFF_T | distance | q+o == p |
streamsize(o) | streamsize | converts | streamsize(O(sz)) == sz |
O(sz) | OFF_T | converts | streamsize(O(sz)) == sz |
[Note:
Every implementation is required to supply overloaded operators on
fpos
objects to satisfy the requirements of lib.fpos.operations.
It is unspecified whether these operators are members of
fpos,
global operators,
or provided in some other way.
--- end note]
-2-
Stream operations that return a value of type
traits::pos_type
return
P(O(-1))
as an invalid value to signal an error.
If this value is used as an argument to any
istream,
ostream,
or
streambufmember
that accepts a value of type
traits::pos_type
then the behavior of that function is undefined.
27.4.4 - Template class basic_ios [lib.ios]
namespace std { template <class charT, class traits = char_traits<charT> > class basic_ios : public ios_base { public: //Types: typedef charT char_type; typedef typename traits::int_type int_type; typedef typename traits::pos_type pos_type; typedef typename traits::off_type off_type; typedef traits traits_type;
operator void*() const bool operator!() const iostate rdstate() const; void clear(iostate state = goodbit); void setstate(iostate state); bool good() const; bool eof() const; bool fail() const; bool bad() const;
iostate exceptions() const; void exceptions(iostate except);
//lib.basic.ios.cons Constructor/destructor: explicit basic_ios(basic_streambuf<charT,traits>* sb); virtual ~basic_ios();
//lib.basic.ios.members Members: basic_ostream<charT,traits>* tie() const; basic_ostream<charT,traits>* tie(basic_ostream<charT,traits>* tiestr);
basic_streambuf<charT,traits>* rdbuf() const; basic_streambuf<charT,traits>* rdbuf(basic_streambuf<charT,traits>* sb);
basic_ios& copyfmt(const basic_ios& rhs);
char_type fill() const; char_type fill(char_type ch);
//lib.ios.base.locales locales: locale imbue(const locale& loc);
char narrow(char_type c, char dfault) const; char_type widen(char c) const;
protected: basic_ios(); void init(basic_streambuf<charT,traits>* sb);
private: basic_ios(const basic_ios& ); //not defined basic_ios& operator=(const basic_ios&); // not defined }; }
explicit basic_ios(basic_streambuf<charT,traits>* sb);
-1- Effects:
Constructs an object of class
basic_ios,
assigning initial values to its member objects by calling
init(sb).
basic_ios();
-2- Effects:
Constructs an object of class
basic_ios
(lib.ios.base.cons)
leaving its member objects uninitialized. The object
must be initialized by calling its
init
member function. If it is destroyed before it has been initialized the
behavior is undefined.
void init(basic_streambuf<charT,traits>* sb);
-3- Postconditions: The postconditions of this function are indicated in Table ??:
Element | Value |
rdbuf() | sb |
tie() | 0 |
rdstate() | goodbit if sb is not a null pointer, otherwise badbit . |
exceptions() | goodbit |
flags() | skipws | dec |
width() | 0 |
precision() | 6 |
fill() | widen(' '); |
getloc() | a copy of the value returned by locale() |
iarray | a null pointer |
parray | a null pointer |
27.4.4.2 - Member functions [lib.basic.ios.members]
basic_ostream<charT,traits>* tie() const;
-1- Returns:
An output sequence that is
tied
to (synchronized with) the sequence controlled by the stream buffer.
basic_ostream<charT,traits>* tie(basic_ostream<charT,traits>* tiestr);
-2- Postcondition: tiestr == tie().
-3- Returns:
The previous value of
tie().
basic_streambuf<charT,traits>* rdbuf() const;
-4- Returns:
A pointer to the
streambuf
associated with the stream.
basic_streambuf<charT,traits>* rdbuf(basic_streambuf<charT,traits>* sb);
-5- Postcondition: sb == rdbuf().
-6- Effects: Calls clear().
-7- Returns:
The previous value of
rdbuf().
// lib.ios.base.locales locales:
locale imbue(const locale& loc);
-8- Effects: Calls ios_base::imbue(loc) (lib.ios.base.locales) and if rdbuf()!=0 then rdbuf()->pubimbue(loc) (lib.streambuf.locales).
-9- Returns:
The prior value of
ios_base::imbue().
char narrow(char_type c, char dfault) const;
-10- Returns:
use_facet< ctype<char_type> >(getloc()).narrow(c,dfault)
char_type widen(char c) const;
-11- Returns:
use_facet< ctype<char_type> >(getloc()).widen(c)
char_type fill() const
-12- Returns:
The character used to pad (fill) an output conversion to the specified
field width.
char_type fill(char_type fillch);
-13- Postcondition: fillch == fill()
-14- Returns:
The previous value of
fill().
basic_ios& copyfmt(const basic_ios& rhs);
-15- Effects: Assigns to the member objects of *this the corresponding member objects of rhs , except that:
[Footnote: This suggests an infinite amount of copying, but the implementation can keep track of the maximum element of the arrays that is non-zero. --- end foonote]
-16- If any newly stored pointer values in *this point at objects stored outside the object rhs , and those objects are destroyed when rhs is destroyed, the newly stored pointer values are altered to point at newly constructed copies of the objects.
-17- Before copying any parts of rhs, calls each registered callback pair (fn,index) as (*fn)(erase_event,*this,index). After all parts but exceptions() have been replaced, calls each callback pair that was copied from rhs as (*fn)(copy_event,*this,index).
-18- Notes: The second pass permits a copied pword value to be zeroed, or its referent deep copied or reference counted or have other special action taken.
-19- Returns:
*this.
27.4.4.3 - basic_ios iostate flags functions [lib.iostate.flags]
operator void*() const
-1- Returns:
If
fail()
then a null pointer;
otherwise some non-null pointer to indicate success.
bool operator!() const
-2- Returns:
fail().
iostate rdstate() const;
-3- Returns:
The error state of the stream buffer.
void clear(iostate state = goodbit);
-4- Postcondition: If rdbuf()!=0 then state == rdstate(); otherwise rdstate()==state|ios_base::badbit.
-5- Effects:
If
(rdstate() & exceptions()) == 0,
returns.
Otherwise, the function throws an object fail of class
basic_ios::failure
(lib.ios::failure),
constructed with implementation-defined argument values.
void setstate(iostate state);
-6- Effects:
Calls
clear(rdstate() | state)
(which may throw
basic_ios::failure
(lib.ios::failure)).
bool good() const;
-7- Returns:
rdstate() == 0
bool eof() const;
-8- Returns:
true
if
eofbit
is set in
rdstate().
bool fail() const;
-9- Returns: true if failbit or badbit is set in rdstate().*
[Footnote: Checking badbit also for fail() is historical practice. --- end foonote]
bool bad() const;
-10- Returns:
true
if
badbit
is set in
rdstate().
iostate exceptions() const;
-11- Returns:
A mask that determines what elements set in
rdstate()
cause exceptions to be thrown.
void exceptions(iostate except);
-12- Postcondition: except == exceptions().
-13- Effects:
Calls
clear(rdstate()).
27.4.5 - ios_base manipulators [lib.std.ios.manip]
27.4.5.1 - fmtflags manipulators [lib.fmtflags.manip]
ios_base& boolalpha(ios_base& str);
-1- Effects: Calls str.setf(ios_base::boolalpha).
-2- Returns:
str .
ios_base& noboolalpha(ios_base& str);
-3- Effects: Calls str.unsetf(ios_base::boolalpha).
-4- Returns:
str .
ios_base& showbase(ios_base& str);
-5- Effects: Calls str.setf(ios_base::showbase).
-6- Returns:
str .
ios_base& noshowbase(ios_base& str);
-7- Effects: Calls str.unsetf(ios_base::showbase).
-8- Returns:
str .
ios_base& showpoint(ios_base& str);
-9- Effects: Calls str.setf(ios_base::showpoint).
-10- Returns:
str .
ios_base& noshowpoint(ios_base& str);
-11- Effects: Calls str.unsetf(ios_base::showpoint).
-12- Returns:
str .
ios_base& showpos(ios_base& str);
-13- Effects: Calls str.setf(ios_base::showpos).
-14- Returns:
str .
ios_base& noshowpos(ios_base& str);
-15- Effects: Calls str.unsetf(ios_base::showpos).
-16- Returns:
str .
ios_base& skipws(ios_base& str);
-17- Effects: Calls str.setf(ios_base::skipws).
-18- Returns:
str .
ios_base& noskipws(ios_base& str);
-19- Effects:
Calls
str.unsetf(ios_base::skipws).
-1- Effects:
Calls
str.setf(ios_base::internal, ios_base::adjustfield).
-2- Returns:
str .
-3- Effects:
Calls
str.setf(ios_base::left, ios_base::adjustfield).
-4- Returns:
str .
-5- Effects:
Calls
str.setf(ios_base::right, ios_base::adjustfield).
-6- Returns:
str .
-1- Effects:
Calls
str.setf(ios_base::dec, ios_base::basefield).
-2- Returns:
str .
-3- Effects:
Calls
str.setf(ios_base::hex, ios_base::basefield).
-4- Returns:
str .
-5- Effects:
Calls
str.setf(ios_base::oct, ios_base::basefield).
-6- Returns:
str .
-1- Effects:
Calls
str.setf(ios_base::fixed, ios_base::floatfield).
-2- Returns:
str .
-3- Effects:
Calls
str.setf(ios_base::scientific, ios_base::floatfield).
-4- Returns:
str .
Header <streambuf> synopsis
-1-
The header
<streambuf>
defines types that control input from and output to
character
sequences.
-1-
Stream buffers can impose various constraints on the sequences they control.
Some constraints are:
-2-
Each sequence is characterized by three pointers which, if non-null,
all point into the same
charT
array object.
The array object represents, at any moment,
a (sub)sequence of characters from the sequence.
Operations performed on a sequence alter the values stored in these pointers,
perform reads and writes directly to or from associated sequences, and
alter ``the stream position'' and conversion state as needed to
maintain this subsequence relationship.
The three pointers are:
-3-
The following semantic constraints shall always apply for any set of
three pointers for a sequence, using the pointer names given immediately above:
-1-
The class template
basic_streambuf<charT,traits>
serves as an abstract base class for deriving various
stream buffers
whose objects each control two
character sequences:
-2-
The class
streambuf
is a specialization of the template class
basic_streambuf
specialized for the type
char.
-3-
The class
wstreambuf
is a specialization of the template class
basic_streambuf
specialized for the type
wchar_t.
-1- Effects:
Constructs an object of class
basic_streambuf<charT,traits>
and initializes:*
-2- Notes:
Once the
getloc()
member is initialized, results of calling locale member functions,
and of members of facets so obtained, can safely be cached until the
next time the member
imbue
is called.
-1- Postcondition:
loc == getloc().
-2- Effects:
Calls
imbue(loc).
-3- Returns:
Previous value of
getloc().
-4- Returns:
If
pubimbue()
has ever been called, then the last value of loc supplied,
otherwise the current global locale,
locale(),
in effect at the time of construction.
If called after
pubimbue()
has been called but before
pubimbue
has returned (i.e. from within the call of
imbue())
then it returns the previous value.
-1- Returns:
setbuf( s , n ).
-2- Returns:
seekoff( off , way , which ).
-3- Returns:
seekpos( sp , which ).
-4- Returns:
sync().
-1- Returns:
If a read position is available, returns
egptr() - gptr().
Otherwise returns
showmanyc()
(lib.streambuf.virt.get).
-2- Effects:
Calls
sbumpc().
-3- Returns:
if that function returns
traits::eof(),
returns
traits::eof().
Otherwise, returns
sgetc().
-4- Returns:
If the input sequence read position is not available,
returns
uflow().
Otherwise, returns
traits::to_int_type(*gptr())
and increments the next pointer for the input sequence.
-5- Returns:
If the input sequence read position is not available,
returns
underflow().
Otherwise, returns
traits::to_int_type(*gptr()).
-6- Returns:
xsgetn( s , n ).
-1- Returns:
If the input sequence putback position is not available, or
if
traits::eq(c,gptr()[-1])
is false, returns
pbackfail(traits::to_int_type(c)).
Otherwise, decrements the next pointer for the input sequence and
returns
traits::to_int_type(*gptr()).
-2- Returns:
If the input sequence putback position is not available,
returns
pbackfail().
Otherwise, decrements the next pointer for the input sequence and
returns
traits::to_int_type(*gptr()).
-1- Returns:
If the output sequence write position is not available,
returns
overflow(traits::to_int_type(c)).
Otherwise, stores c at the next pointer for the output sequence,
increments the pointer, and
returns
traits::to_int_type(c).
-2- Returns:
xsputn(s,n).
-1- Returns:
The beginning pointer for the input sequence.
-2- Returns:
The next pointer for the input sequence.
-3- Returns:
The end pointer for the input sequence.
-4- Effects:
Advances the next pointer for the input sequence by
n .
-5- Postconditions:
gbeg == eback(),
gnext == gptr(),
and
gend == egptr().
-1- Returns:
The beginning pointer for the output sequence.
-2- Returns:
The next pointer for the output sequence.
-3- Returns:
The end pointer for the output sequence.
-4- Effects:
Advances the next pointer for the output sequence by n .
-5- Postconditions:
pbeg == pbase(),
pbeg == pptr(),
and
pend == epptr().
-1- Effects:
Change any translations based on locale.
-2- Notes:
Allows the derived class to be informed of changes in locale at the
time they occur.
Between invocations of this function a class derived
from streambuf can safely cache results of calls to locale functions
and to members of facets so obtained.
-3- Default behavior:
Does nothing.
-1- Effects:
Performs an operation that is defined separately for each class
derived from
basic_streambuf
in this clause
(lib.stringbuf.virtuals,
lib.filebuf.virtuals).
-2- Default behavior:
If
gptr()
is non-null and
gptr()!=egptr()
then do nothing.
Returns
this.
-3- Effects:
Alters the stream positions within one or more of
the controlled sequences in a way that is defined separately for each class
derived from
basic_streambuf
in this clause
(lib.stringbuf.virtuals,
lib.filebuf.virtuals).
-4- Default behavior:
Returns an object of class
pos_type
that stores an
invalid stream position
(_lib.iostreams.definitions_).
-5- Effects:
Alters the stream positions within one or more of
the controlled sequences in a way that is defined separately for each class
derived from
basic_streambuf
in this clause
(lib.stringbuf,
lib.filebuf).
-6- Default behavior:
Returns an object of class
pos_type
that stores an
invalid stream position.
-7- Effects:
Synchronizes the controlled sequences with the arrays.
That is, if
pbase()
is non-null the characters between
pbase()
and
pptr()
are written to the controlled sequence.
The pointers may then be reset as appropriate.
-8- Returns:
-1 on failure.
What constitutes failure is determined by each derived class (lib.filebuf.virtuals).
-9- Default behavior:
Returns zero.
-1- Returns:
an estimate of the number of
characters available in the sequence, or -1.
If it returns
a positive value, then successive calls to
underflow()
will not return
traits::eof()
until at least that number of characters have been supplied.
If
showmanyc()
returns -1, then calls to
underflow()
or
uflow()
will fail.*
-2- Default behavior:
Returns zero.
-3- Notes:
Uses
traits::eof().
-4- Effects:
Assigns up to n characters to successive elements of
the array whose first element is designated by s .
The characters assigned are read from the input sequence as if
by repeated calls to
sbumpc().
Assigning stops when either n characters
have been assigned or a call to
sbumpc()
would return
traits::eof().
-5- Returns:
The number of characters assigned.*
-6- Notes:
Uses
traits::eof().
-7- Notes:
The public members of
basic_streambuf
call this virtual function only if
gptr()
is null or
gptr() >= egptr()
-8- Returns:
traits::to_int_type(c),
where c is the first
character
of the
pending sequence,
without moving the input sequence position past it.
If the pending sequence is null then the function returns
traits::eof()
to indicate failure.
-9-
The
pending sequence
of characters is defined as the concatenation of:
-10-
The
result character
is
-11-
The
backup sequence
is defined as the concatenation of:
-12- Effects:
The function sets up the
gptr()
and
egptr()
satisfying one of:
-13-
If
eback()
and
gptr()
are non-null then the function is not constrained as to their contents, but the ``usual backup condition'' is that either:
-14- Default behavior:
Returns
traits::eof().
-15- Requires:
The constraints are the same as for
underflow(),
except that the result character is transferred from the pending
sequence to the backup sequence, and the pending sequence may not
be empty before the transfer.
-16- Default behavior:
Calls
underflow().
If
underflow()
returns
traits::eof(),
returns
traits::eof().
Otherwise, returns the value of
traits::to_int_type(*gptr())
and increment the value of the next pointer for the input sequence.
-17- Returns:
traits::eof()
to indicate failure.
-1- Notes:
The public functions of
basic_streambuf
call this virtual function only when
gptr()
is null,
gptr() == eback(),
or
traits::eq(*gptr(),traits::to_char_type(c))
returns false.
Other calls shall also satisfy that constraint.
-2- Postcondition:
On return, the constraints of
gptr(),
eback(),
and
pptr()
are the same as for
underflow().
-3- Returns:
traits::eof()
to indicate failure.
Failure may occur because the input sequence could not be backed up, or if for some
other reason the pointers could not be set consistent with the constraints.
pbackfail()
is called only when put back has really failed.
-4- Default behavior:
Returns
traits::eof().
-1- Effects:
Writes up to n characters to the output sequence as if
by repeated calls to
sputc( c ).
The characters written are obtained from successive elements of
the array whose first element is designated by s .
Writing stops when either n characters have been written or
a call to
sputc( c )
would return
traits::eof().
-2- Returns:
The number of characters written.
-3- Effects:
Consumes some initial subsequence of the characters of the
pending sequence.
The pending sequence is defined as the concatenation of
-4- Notes:
The member functions
sputc()
and
sputn()
call this function in case that
no room can be found in the put buffer enough to accomodate the
argument character sequence.
-5- Requires:
Every overriding definition of this virtual function
shall obey the following constraints:
-6- Returns:
traits::eof()
or throws an exception
if the function fails.
-7- Default behavior:
Returns
traits::eof().
Header <istream> synopsis
Header <ostream> synopsis
Header <iomanip> synopsis
-1-
The header
<istream>
defines two types
and a function signature that control input from a stream buffer.
-1-
The class
basic_istream
defines a number of member function
signatures that assist in reading and interpreting input from sequences
controlled by a stream buffer.
-2-
Two groups of member function signatures share common properties:
the
formatted input functions
(or
extractors)
and the
unformatted input functions.
Both groups of input functions are described as if they obtain (or
extract)
input
characters
by calling
rdbuf()->sbumpc()
or
rdbuf()->sgetc().
They may use other public members of
istream.
-3-
If
rdbuf()->sbumpc()
or
rdbuf()->sgetc()
returns
traits::eof(),
then the input function, except as explicitly noted otherwise, completes its actions and does
setstate(eofbit),
which may throw
ios_base::failure
(lib.iostate.flags), before returning.
-4-
If one of these called functions throws an exception, then unless explicitly noted otherwise,
the input function sets
badbit
in error state.
If
badbit
is on in
exception(),
the input function
rethrows the exception without completing its actions, otherwise
it does not throw anything and proceeds as if the called function had returned
a failure indication.
-1- Effects:
Constructs an object of class
basic_istream,
assigning initial values to the base class by calling
basic_ios::init(sb)
(lib.basic.ios.cons).
-2- Postcondition:
gcount() == 0
-3- Effects:
Destroys an object of class
basic_istream.
-4- Notes:
Does not perform any operations of
rdbuf().
-1-
The class
sentry
defines a class that is responsible for doing exception safe prefix and suffix
operations.
-2- Effects:
If
is.good()
is
true,
prepares for formatted or
unformatted input.
First, if
is.tie()
is not a null pointer, the
function calls
is.tie()->flush()
to synchronize the output sequence with any associated external
C stream.
Except that this call can be suppressed if the put area of
is.tie()
is empty.
Further an implementation is allowed to defer the call to flush until a
call of
is->rdbuf()->underflow
occurs.
If no such call occurs before the
sentry
object is destroyed, the call to
flush
may be eliminated entirely*
-3- Notes:
The constructor
explicit sentry(basic_istream<charT,traits>& is, bool noskipws = false)
uses the currently imbued locale in is ,
to determine whether the next input character is
whitespace or not.
-4-
To decide if the character c is a whitespace character,
the constructor performs ``as if'' it executes the following code fragment:
-5-
If, after any preparation is completed,
is.good()
is
true,
ok_ != false
otherwise,
ok_ == false.
During preparation, the constructor may call
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags))*
-6-
[Example:
A typical implementation of the
sentry
constructor might include code such as:
-7- Effects:
None.
-8- Effects:
Returns
ok_.
-1-
Each formatted input function begins execution by constructing an object of class
sentry
with the
noskipws
(second) argument
false.
If the
sentry
object returns
true,
when converted to a value of type
bool,
the function endeavors
to obtain the requested input.
If an exception is thrown during input then
ios::badbit
is turned on* in
*this's
error state.
-1- Returns:
pf(*this).*
-2- Effects:
Calls
pf(*this).
-3- Returns:
*this.
-4- Effects:
Calls
pf(*this).*
-5- Returns:
*this.
-6- Effects:
Behaves like a formatted input member (as described in lib.istream.formatted.reqmts)
of in.
After a
sentry
object is constructed,
operator>>
extracts characters and stores them into
successive locations of an array whose first element is designated by
s .
If
width()
is greater than zero, n is
width().
Otherwise n is the the number of elements
of the largest array of
char_type
that can store a terminating
eos.
n is the maximum number of characters stored.
-7-
Characters are extracted and stored until any of the following occurs:
-8-
If the function extracted no characters, it calls
setstate(failbit),
which may throw
ios_base::failure
(lib.iostate.flags).
-9- Returns:
in.
-10- Effects:
Behaves like a formatted input member (as described in lib.istream.formatted.reqmts)
of in.
After a
sentry
object is constructed
a character is extracted from in, if one is available, and stored in c .
Otherwise, the function calls
in.setstate(failbit).
-11- Returns:
in.
-12- Effects:
If sb is null, calls
setstate(failbit),
which may throw
ios_base::failure
(lib.iostate.flags).
-13-
If the function inserts no characters, it calls
setstate(failbit),
which may throw
ios_base::failure
(lib.iostate.flags).
If failure was due to catching an exception thrown while extracting characters from
sb
and
failbit
is on in
exceptions()
(lib.iostate.flags),
then the caught exception is rethrown.
-14- Returns:
*this.
-1-
Each unformatted input function begins execution by constructing an object of class
sentry
with the default argument
noskipws
(second) argument
true.
If the
sentry
object returns
true,
when converted to a value of type
bool,
the function endeavors
to obtain the requested input.
If an exception is thrown during input then
ios::badbit
is turned on* in
*this's
error state.
-2- Returns:
The number of characters
extracted by the last unformatted input member function called for the object.
-3- Effects:
Extracts a character c , if one is available.
Otherwise, the function calls
setstate(failbit),
which may throw
ios_base::failure
(lib.iostate.flags),
-4- Returns:
c if available,
otherwise
traits::eof().
-5- Effects:
Extracts a character, if one is available, and assigns it to c .*
-6- Returns:
*this.
-7- Effects:
Extracts characters and stores them
into successive locations of an array whose first element is designated by
s .*
-8-
If the function stores no characters, it calls
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).
In any case, it then stores a null character
into the next successive location of the array.
-9- Returns:
*this.
-10- Effects:
Calls
get(s,n,widen('\n'))
-11- Returns:
Value returned by the call.
-12- Effects:
Extracts characters and inserts them
in the output sequence controlled by
sb.
Characters are extracted and inserted until any of the following occurs:
-13-
If the function inserts no characters, it calls
setstate(failbit),
which may throw
ios_base::failure
(lib.iostate.flags).
-14- Returns:
*this.
-15- Effects:
Calls
get(s,n,widen('\n'))
-16- Returns:
Value returned by the call.
-17- Effects:
Extracts characters and stores them
into successive locations of an array whose first element is designated by
s .*
-18-
These conditions are tested in the order shown.*
-19-
If the function extracts no characters, it calls
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).*
-1- Effects:
Extracts characters as long as the next available character c
is whitespace or until there are no more characters in the sequence.
Whitespace characters are distinguished with the same criterion as used
by
sentry::sentry
(lib.istream::sentry).
If
ws
stops extracting characters because there are no more available it sets
eofbit,
but not
failbit.
-2- Returns:
is .
-1-
The class
basic_iostream
inherits a number of functions that allow reading input and writing output to
sequences controlled by a stream buffer.
-1- Effects
Constructs an object of class
basic_iostream,
assigning initial values to the base classes by calling
basic_istream<charT,traits>(sb)
(lib.istream)
and
basic_ostream<charT,traits>(sb)
(lib.ostream)
-2- Postcondition:
rdbuf()==sb
and
gcount()==0.
-1- Effects:
Destroys an object of class
basic_iostream.
-2- Notes:
Does not perform any operations on
rdbuf().
-1-
The header
<ostream>
defines a type
and several function signatures that control output to a
stream buffer.
-1-
The class
basic_ostream
defines a number of member function
signatures that assist in formatting and writing output to output sequences
controlled by a stream buffer.
-2-
Two groups of member function signatures share common properties:
the
formatted output functions
(or
inserters)
and the
unformatted output functions.
Both groups of output functions generate (or
insert)
output
characters
by actions equivalent to calling
rdbuf().sputc(int_type).
They may use other public members of
basic_ostream
except that they do not invoke any virtual members of
rdbuf()
except
overflow().
-3-
If one of these called functions throws an exception, then unless explicitly noted otherwise
the output function set
badbit
in error state.
If
badbit
is on in
exception(),
the output function
rethrows the exception without completing its actions, otherwise
it does not throw anything and treat as an error.
-1- Effects:
Constructs an object of class
basic_ostream,
assigning initial values to the base class by calling
basic_ios<charT,traits>::init(sb)
(lib.basic.ios.cons).
-2- Postcondition:
rdbuf() == sb.
-3- Effects:
Destroys an object of class
basic_ostream.
-4- Notes:
Does not perform any operations on
rdbuf().
-1-
The class
sentry
defines a class that is responsible for doing exception safe prefix and suffix
operations.
-2-
If
os.good()
is nonzero, prepares for formatted or unformatted output.
If
os.tie()
is not a null pointer, calls
os.tie()->flush().*
-3-
If, after any preparation is completed,
os.good()
is
true,
ok_ == true
otherwise,
ok_ == false.
During preparation, the constructor may call
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags))*
-4-
If
((os.flags() & ios_base::unitbuf) && !uncaught_exception())
is
true,
calls
os.flush().
-5- Effects:
Returns
ok_.
-1- Returns:
if
fail() != false,
returns
pos_type(-1)
to indicate failure.
Otherwise, returns
rdbuf()->pubseekoff(0, cur, out).
-2- Effects:
If
fail() != true,
executes
rdbuf()->pubseekpos(pos).
-3- Returns:
*this.
-4- Effects:
If
fail() != true,
executes
rdbuf()->pubseekoff(off, dir).
-5- Returns:
*this.
-1-
Each formatted output function begins execution by constructing an object of class
sentry.
If this object returns
true
when converted to a value of type
bool,
the function endeavors
to generate the requested output.
If the generation fails, then the formatted output function does
setstate(ios::failbit),
which might throw an exception.
If an exception is thrown during output, then
ios::badbit
is turned on* in
*this's
error state.
-2-
The descriptions of the individual formatted output operations
describe how they perform
output and do not mention the
sentry
object.
-1- Effects:
The classes
num_get<>
and
num_put<>
handle locale-dependent numeric formatting and
parsing.
These inserter functions use the imbued
locale
value to perform numeric formatting.
The formatting conversion occurs as if it performed the following
code fragment:
-2- Returns:
*this.
-1- Returns:
pf(*this).*
-2- Effects:
Calls
pf(*this).
-3- Returns:
*this.*
-4- Effects:
Calls
pf(*this).
-5- Returns:
*this.
-6- Effects:
If sb is null calls
setstate(badbit)
(which may throw
ios_base::failure).
-7-
Gets characters from sb
and inserts them in
*this.
Characters are read from sb
and inserted until any of the following occurs:
-8-
If the function inserts no characters, it calls
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).
If an exception was thrown while extracting a character,
the function set
failbit
in error state, and if
failbit
is on in
exceptions()
the caught exception is rethrown.
-9- Returns:
*this.
-1- Effects:
Behaves like an formatted inserter (as described in lib.ostream.formatted.reqmts) of out.
After a
sentry
object is constructed it inserts characters.
In case c has type
char
and the character type of the stream is not
char,
then the character to be inserted is
out.widen(c);
otherwise the character is c*
-2- Returns:
out
-3- Requires:
s is non-null.
-4- Effects:
Behaves like an formatted inserter (as described in lib.ostream.formatted.reqmts) of out.
After a
sentry
object is constructed it inserts characters.
The number of characters starting at s to be inserted is
traits::length(s).
Padding is determined as described in lib.facet.num.put.virtuals.
The
traits::length(s)
characters starting at s are widened using
out.widen
(lib.basic.ios.members).
The widened characters and any required padding are inserted into
out.
Calls
width(0).
-5- Returns:
out
-1-
Each formatted output function begins execution by constructing an object of class
sentry.
If this object returns
true,
while converting to a value of type
bool,
the function endeavors
to generate the requested output.
In any case, the unformatted output function ends
by destroying the
sentry
object,
then returning the value specified for the formatted output function.
-2- Effects:
Inserts the character c , if possible.*
-3-
Otherwise, calls
setstate(badbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).
-4- Returns:
*this.
-5- Effects:
Obtains characters to insert from
successive locations of an array whose first element is designated by
s .*
-6- Returns:
*this.
-7-
If
rdbuf()
is not a null pointer,
calls
rdbuf()->pubsync().
If that function returns -1
calls
setstate(badbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).
-8- Returns:
*this.
-1- Effects:
Calls
os.put(os.widen('\n') ),
then
os.flush().
-2- Returns:
os .*
-3- Effects:
Inserts a null character into the output sequence:
calls
os.put(charT()).
-4- Returns:
os .
-5- Effects:
Calls
os.flush().
-6- Returns:
os .
-1-
The header
<iomanip>
defines a type and several related functions that use this type
to provide extractors and inserters that
alter information maintained by class
ios_base
and its derived classes.
-2-
The type designated
smanip
in each of the following function descriptions is implementation-specified and may be
different for each function.
-3- Returns:
An object s of unspecified type such that if
out
is an (instance of)
basic_ostream
then the expression
out<<s
behaves as if
f(s)
were called,
and if
in
is an (instance of)
basic_istream
then the expression
in>>s
behaves as if
f(s)
were called.
Where f can be defined as:*
-4- Returns:
An object s of unspecified type such that if
out
is an (instance of)
basic_ostream
then the expression
out<<s
behaves as if
f(s)
were called,
in
is an (instance of)
basic_istream
then the expression
in>>s
behaves as if
f(s)
were called.
Where f can be defined as:
-5- Returns:
An object s of unspecified type such that if
out
is an (instance of)
basic_ostream
then the expression
out<<s
behaves as if
f(s)
were called,
in
is an (instance of)
basic_istream
then the expression
in>>s
behaves as if
f(s)
were called.
Where f can be defined as:
-6- Returns:
An object s of unspecified type such that if
out
is (or is derived from)
basic_ostream<charT,traits>
and c has type
charT
then the expression
out<<s
behaves as if
f(s)
were called,
where f can be defined as:
-7- Returns:
An object s of unspecified type such that if
out
is an (instance of)
basic_ostream
then the expression
out<<s
behaves as if
f(s)
were called,
in
is an (instance of)
basic_istream
then the expression
in>>s
behaves as if
f(s)
were called.
Where f can be defined as:
-8- Returns:
An object s of unspecified type such that if
out
is an (instance of)
basic_ostream
then the expression
out<<s
behaves as if
f(s)
were called,
in
is an (instance of)
basic_istream
then the expression
in>>s
behaves as if
f(s)
were called.
Where f can be defined as:
-1-
The header
<sstream>
defines four template classes, and six types, that associate stream buffers with objects of class
basic_string,
as described in lib.string.classes.
Header <sstream> synopsis
-1-
The class
basic_stringbuf
is derived from
basic_streambuf
to associate possibly the input sequence and possibly
the output sequence with a sequence of arbitrary
characters.
The sequence can be initialized from, or made available as, an object of class
basic_string.
-2-
For the sake of exposition, the maintained data is presented here as:
-1- Effects:
Constructs an object of class
basic_stringbuf,
initializing the base class with
basic_streambuf()
(lib.streambuf.cons), and initializing
mode with which .
-2- Notes:
The function allocates no array object.
-3- Effects:
Constructs an object of class
basic_stringbuf,
initializing the base class with
basic_streambuf()
(lib.streambuf.cons), and initializing
mode with which .
Then copies the content of
str
into the
basic_stringbuf
underlying character sequence and initializes the input and output
sequences according to
which.
If
which & ios_base::out
is true, initializes the output sequence with the underlying sequence.
If
which & ios_base::in
is true, initializes the input sequence with the underlying sequence.
-4- Postconditions:
str() == str.
-1- Returns:
A
basic_string
object whose content is equal to the
basic_stringbuf
underlying character sequence.
If the buffer is only created in input mode, the underlying character sequence
is equal to the input sequence; otherwise, it is equal to the output sequence.
In case of an empty underlying character sequence, the function returns
basic_string<charT,traits,Allocator>().
-2- Effects:
If the
basic_stringbuf's
underlying character sequence is not empty, deallocates it.
Then copies the content of
s
into the
basic_stringbuf
underlying character sequence and initializes the input and output sequences
according to the mode stored when creating the
basic_stringbuf
object.
If
(mode&ios_base::out)
is true,
then initializes the output sequence with the underlying sequence.
If
(mode&ios_base::in)
is true, then initializes the input sequence with the underlying sequence.
-3- Postcondition:
str() == s.
-1- Returns:
If the input sequence has a read position available,
returns
traits::to_int_type(*gptr()).
-2- Effects:
Puts back the character designated by c to the input
sequence, if possible, in one of three ways:
-3- Returns:
traits::eof()
to indicate failure.
-4- Notes:
If the function can succeed in more than one of these ways, it is
unspecified which way is chosen.
-5- Effects:
Appends the character designated by c to the output
sequence, if possible, in one of two ways:
-6- Notes:
The function can alter the number of write positions available as a
result of any call.
-7- Returns:
traits::eof()
to indicate failure.
-8-
-9- Notes:
The function can make a write position available only if
( mode & ios_base::out) != 0.
To make a write position available,
the function reallocates (or initially allocates) an array object
with a sufficient number of elements to hold
the current array object (if any), plus one additional write position.
If
( mode & ios_base::in) != 0,
the function alters the read end pointer
egptr()
to point just past the new write position
(as does the write end pointer
epptr()).
-10- Effects:
Alters the stream position within one of the
controlled sequences, if possible, as indicated in Table ??:
-11-
For a sequence to be positioned, if its next pointer
(either
gptr()
or
pptr())
is a null pointer, the positioning operation fails.
Otherwise, the function determines newoff as indicated in Table ??:
-12-
_
If
(newoff + off) < 0,
or
(xend - xbeg) < (newoff + off),
the positioning operation fails.
Otherwise, the function assigns
xbeg + newoff + off
to the next pointer xnext .
-13- Returns:
pos_type(newoff),
constructed from the resultant offset newoff
(of type
off_type),
that stores the resultant stream position, if possible.
If the positioning operation fails, or
if the constructed object cannot represent the resultant stream position,
the object stores an invalid stream position.
-14- Effects:
Alters the stream position within the
controlled sequences, if possible, to correspond to the
stream position stored in sp
(as described below).
-15- Returns:
sp
to indicate success, or
pos_type(off_type(-1))
to indicate failure.
-16- Effects:
implementation-defined, except that
setbuf(0,0)
has no effect.
-17- Returns:
this.
-1-
The class
basic_istringstream<charT,traits,Allocator>
supports reading objects of class
basic_string<charT,traits,Allocator>.
It uses a
basic_stringbuf<charT,traits,Allocator>
object to control the associated storage.
For the sake of exposition, the maintained data is presented here as:
-1- Effects:
Constructs an object of class
basic_istringstream<charT,traits>,
initializing the base class with
basic_istream(&sb)
and initializing sb with
basic_stringbuf<charT,traits,Allocator>(which|ios_base::in))
(lib.stringbuf.cons).
-2- Effects:
Constructs an object of class
basic_istringstream<charT,traits>,
initializing the base class with
basic_istream(&sb)
and initializing sb with
basic_stringbuf<charT,traits,Allocator>(str, which | ios_base::in))
(lib.stringbuf.cons).
-1- Returns:
(basic_stringbuf<charT,traits,Allocator>*)&sb .
-2- Returns:
rdbuf()->str().*
-3- Effects:
Calls
rdbuf()->str(s).
-1-
The class
basic_ostringstream<charT,traits,Allocator>
supports writing objects of class
basic_string<charT,traits,Allocator>.
It uses a
basic_stringbuf
object to control the associated storage.
For the sake of exposition, the maintained data is presented here as:
-1- Effects:
Constructs an object of class
basic_ostringstream,
initializing the base class with
basic_ostream(&sb)
and initializing sb with
basic_stringbuf<charT,traits,Allocator>(which | ios_base::out))
(lib.stringbuf.cons).
-2- Effects:
Constructs an object of class
basic_ostringstream<charT,traits>,
initializing the base class with
basic_ostream(&sb)
and initializing sb with
basic_stringbuf<charT,traits,Allocator>(str, which | ios_base::out))
(lib.stringbuf.cons).
-1- Returns:
(basic_stringbuf<charT,traits,Allocator>*)&sb .
-2- Returns:
rdbuf()->str().*
-3- Effects:
Calls
rdbuf()->str(s).
-1-
The template class
basic_stringstream<charT,traits>
supports reading and writing from objects of class
basic_string<charT,traits,Allocator>.
It uses a
basic_stringbuf<charT,traits,Allocator>
object to control the associated sequence.
For the sake of exposition, the maintained data is presented here as
-1- Effects:
Constructs an object of class
basic_stringstream<charT,traits>,
initializing the base class with
basic_iostream(&sb)
and initializing
sb
with
basic_stringbuf<charT,traits,Allocator>(which).
-2- Effects:
Constructs an object of class
basic_stringstream<charT,traits>,
initializing the base class with
basic_iostream(&sb)
and initializing
sb
with
basic_stringbuf<charT,traits,Allocator>(str,which).
-1- Returns:
&sb
-2- Returns:
rdbuf()->str().*
-3- Effects:
Calls
rdbuf()->str(str).
-1-
The header
<fstream>
defines four class templates and six types
that associate stream buffers with files and assist
reading and writing files.
Header <fstream> synopsis
-2-
In this subclause, the type name FILE refers to
the type
FILE
defined in
<cstdio>
(lib.c.files).*
-1-
The class
basic_filebuf<charT,traits>
associates both the input sequence and the output
sequence with a file.
-2-
The restrictions on reading and writing a sequence controlled by an
object of class
basic_filebuf<charT,traits>
are the same as for reading and writing with the Standard C library
FILEs.
-3-
In particular:
-4-
An instance of
basic_filebuf
behaves as described in lib.filebuf provided
traits::pos_type
is
fpos<traits::state_type>.
Otherwise the behavior is undefined.
-5-
In order to support file I/O and multibyte/wide character conversion,
conversions are performed using members of a facet, referred to as
a_codecvt in following sections, obtained ``as if'' by
-1- Effects:
Constructs an object of class
basic_filebuf<charT,traits>,
initializing the base class with
basic_streambuf<charT,traits>()
(lib.streambuf.cons).
-2- Postcondition:
is_open() == false.
-3- Effects:
Destroys an object of class
basic_filebuf<charT,traits>.
Calls
close().
-1- Returns:
true
if a previous call to
open
succeeded (returned a non-null value) and there has been no intervening
call to close.
-2- Effects:
If
is_open() != false,
returns a null pointer.
Otherwise,
initializes the
filebuf
as required.
If mode is not some combination of flags shown in the table then
the open fails.
-3-
If the open operation succeeds and
( mode & ios_base::ate) != 0,
positions the file to the end
(``as if'' by calling
std::fseek(file,0,SEEK_END)).*
-4-
If the repositioning operation fails, calls
close()
and returns a null pointer to indicate failure.
-5- Returns:
this
if successful, a null pointer otherwise.
-6- Effects:
If
is_open() == false,
returns a null pointer.
If a put area exists, calls
overflow(EOF)
to flush characters.
If the last virtual member function called on
*this
(between
underflow,
overflow,
seekoff,
and
seekpos)
was
overflow
then calls
a_codecvt.unshift
(possibly several times) to determine a termination sequence, inserts those
characters and calls
overflow(EOF)
again.
Finally it closes the file
(``as if'' by calling
std::fclose(file)).*
-7- Returns:
this
on success, a null pointer otherwise.
-8- Postcondition:
is_open() == false.
-1- Effects:
Behaves the same as
basic_streambuf::showmanyc()
(lib.streambuf.virtuals).
-2- Notes:
An
implementation might well provide an overriding definition for this function
signature if it can determine that more characters can be read from the input
sequence.
-3- Effects:
Behaves according to the description of
basic_streambuf<charT,traits>:: underflow(),
with the specialization that a sequence of characters is read from the input
sequence ``as if'' by reading from the associated file
into an internal buffer (
extern_buf)
and then ``as if'' doing
-4- Effects:
Behaves according to the description of
basic_streambuf<charT,traits>:: uflow(),
with the specialization that a sequence of characters is read from the input
with the same method as used by
underflow.
-5- Effects:
Puts back the character designated by c to the input
sequence, if possible, in one of three ways:
-6- Returns:
traits::eof()
to indicate failure.
-7- Notes:
If
is_open() == false,
the function always fails.
-8- Effects:
Behaves according to the description of
basic_streambuf<charT,traits>::overflow(c),
except that the behavior of ``consuming characters'' is performed by first
coverting ``as if'' by:
-9- Returns:
traits::not_eof(c)
to indicate success, and
traits::eof()
to indicate failure.
If
is_open() == false,
the function always fails.
-10- Effects:
If
setbuf(0,0)
is called on a stream before any I/O has occured on that stream, the
stream becomes unbuffered.
Otherwise the results are implementation-defined.
"Unbuffered" means that
pbase()
and
pptr()
always return null
and output to the file should appear as soon as possible.
-11- Effects:
Let
width
denote
a_codecvt.encoding().
If
is_open() == false,
or
off != 0 && width <= 0,
then the positioning operation fails.
Otherwise, if
way != basic_ios::cur
or
off != 0,
and if the last operation was output,
then update the output sequence and write any unshift sequence.
Next, seek to the new position: if
width > 0,
call
std::fseek(file, width * off, whence),
otherwise call
std::fseek(file, 0, whence).
-12- Notes:
``The last operation was output'' means either
the last virtual operation was overflow or
the put buffer is non-empty.
``Write any unshift sequence'' means,
if
width
if less than zero then call
a_codecvt.unshift(st, xbuf, xbuf+XSIZE, xbuf_end)
and output the resulting unshift sequence.
The function determines one of three values for the
argument whence , of type
int,
as indicated in Table ??:
-13- Returns:
a newly constructed
pos_type
object that stores the resultant
stream position, if possible.
If the positioning operation fails, or
if the object cannot represent the resultant stream position,
returns an invalid stream position (lib.fpos).
-14-
If sp is an invalid stream position, or if the function positions
neither sequence, the positioning operation fails.
If sp has not been obtained by a previous successful call to one of
the positioning functions
(seekoff
or
seekpos)
on the same file the effects are undefined.
-15- Returns:
sp
on success.
Otherwise returns an invalid stream
position (_lib.iostreams.definitions_).
-16- Effects:
If a put area exists, calls
filebuf::overflow
to write the characters to the file.
If a get area exists, the effect is implementation-defined.
-17- Precondition:
If the file is not positioned at its beginning and the encoding of the current
locale as determined by
a_codecvt.encoding()
is state-dependent (lib.locale.codecvt.virtuals)
then that facet is the same as
the corresponding facet of loc.
-18- Effects:
Causes characters inserted or extracted after this call
to be converted according to loc until another call of
imbue.
-19- Note:
This may require reconversion of previously converted characters.
This in turn may require the implementation to be able to reconstruct
the original contents of the file.
-1-
The class
basic_ifstream<charT,traits>
supports reading from named files.
It uses a
basic_filebuf<charT,traits>
object to control the associated
sequence.
For the sake of exposition, the maintained data is presented here as:
-1- Effects:
Constructs an object of class
basic_ifstream<charT,traits>,
initializing the base class with
basic_istream(&sb)
and initializing sb with
basic_filebuf<charT,traits>())
(lib.istream.cons,
lib.filebuf.cons).
-2- Effects:
Constructs an object of class
basic_ifstream,
initializing the base class with
basic_istream(&sb)
and initializing sb with
basic_filebuf<charT,traits>())
(lib.istream.cons,
lib.filebuf.cons),
then calls
rdbuf()->open(s,mode|in).*
-1- Returns:
(basic_filebuf<charT,traits>*)&sb .
-2- Returns:
rdbuf()->is_open().*
-3- Effects:
Calls
rdbuf()->open(s,mode|in).
If that function returns a null pointer,
calls
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).
-4- Effects:
Calls
rdbuf()->close()
and, if that function returns
false,
calls
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).
-1-
The class
basic_ofstream<charT,traits>
supports writing to named files.
It uses a
basic_filebuf<charT,traits>
object to control the associated
sequence.
For the sake of exposition, the maintained data is presented here as:
-1- Effects:
Constructs an object of class
basic_ofstream<charT,traits>,
initializing the base class with
basic_ostream(&sb)
and initializing sb with
basic_filebuf<charT,traits>())
(lib.ostream.cons,
lib.filebuf.cons).
-2- Effects:
Constructs an object of class
basic_ofstream<charT,traits>,
initializing the base class with
basic_ostream(&sb)
and initializing sb with
basic_filebuf<charT,traits>())
(lib.ostream.cons,
lib.filebuf.cons),
then calls
rdbuf()->open(s, mode|out).*
-1- Returns:
(basic_filebuf<charT,traits>*)&sb .
-2- Returns:
rdbuf()->is_open().
-3- Effects:
Calls
rdbuf()->open(s,mode|out).
If that function returns a null pointer,
calls
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).
-4- Effects:
Calls
rdbuf()->close()
and, if that function fails (returns a null pointer), calls
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).
-1-
The template class
basic_fstream<charT,traits>
supports reading and writing from named files.
It uses a
basic_filebuf<charT,traits>
object to control the associated sequences.
For the sake of exposition, the maintained data is presented here as:
-1- Effects:
Constructs an object of class
basic_fstream<charT,traits>,
initializing the base class with
basic_iostream(&sb)
and initializing
sb
with
basic_filebuf<charT,traits>().
-2- Effects:
Constructs an object of class
basic_fstream<charT,traits>,
initializing the base class with
basic_iostream(&sb)
and initializing
sb
with
basic_filebuf<charT,traits>().
Then calls
rdbuf()->open(s,mode).
If that function returns a null pointer, calls
setstate(failbit)
(which may throw
ios_base::failure).
-1- Returns:
&sb
-2- Returns:
rdbuf()->is_open().
-3- Effects:
Calls
rdbuf()->open(s,mode),
If that function returns a null pointer, calls
setstate(failbit),
(which may throw
ios_base::failure).
(lib.iostate.flags) )
-4- Effects:
Calls
rdbuf()->close()
and, if that function returns false, calls
setstate(failbit)(lib.iostate.flags)
(which may throw
ios_base::failure).
-1-
Table ?? describes header
<cstdio>.
See also
ISO C subclause 7.9, Amendment 1 subclause 4.6.2.
ios_base& uppercase(ios_base& str);
ios_base& nouppercase(ios_base& str);
ios_base& unitbuf(ios_base& str);
ios_base& nounitbuf(ios_base& str);
27.4.5.2 - adjustfield manipulators [lib.adjustfield.manip]
ios_base& internal(ios_base& str);
ios_base& left(ios_base& str);
ios_base& right(ios_base& str);
27.4.5.3 - basefield manipulators [lib.basefield.manip]
ios_base& dec(ios_base& str);
[Footnote:
The function signature
dec(ios_base&)
can be called by
the function signature
basic_ostream& stream::operator<<(ios_base& (*)(ios_base&))
to permit expressions of the form
cout << dec
to change the format flags stored in
cout.
--- end foonote]
ios_base& hex(ios_base& str);
ios_base& oct(ios_base& str);
27.4.5.4 - floatfield manipulators [lib.floatfield.manip]
ios_base& fixed(ios_base& str);
ios_base& scientific(ios_base& str);
27.5 - Stream buffers [lib.stream.buffers]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_streambuf;
typedef basic_streambuf<char> streambuf;
typedef basic_streambuf<wchar_t> wstreambuf;
}
27.5.1 - Stream buffer requirements [lib.streambuf.reqts]
27.5.2 - Template class basic_streambuf<charT,traits> [lib.streambuf]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_streambuf {
public:
//
Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
virtual ~basic_streambuf();
//
lib.streambuf.locales locales:
locale pubimbue(const locale &loc);
locale getloc() const;
//
lib.streambuf.buffer buffer and positioning:
basic_streambuf<char_type,traits>*
pubsetbuf(char_type* s, streamsize n);
pos_type pubseekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which =
ios_base::in | ios_base::out);
pos_type pubseekpos(pos_type sp,
ios_base::openmode which =
ios_base::in | ios_base::out);
int pubsync();
//
Get and put areas:
// lib.streambuf.pub.get Get area:
streamsize in_avail();
int_type snextc();
int_type sbumpc();
int_type sgetc();
streamsize sgetn(char_type* s, streamsize n);
//
lib.streambuf.pub.pback Putback:
int_type sputbackc(char_type c);
int_type sungetc();
//
lib.streambuf.pub.put Put area:
int_type sputc(char_type c);
streamsize sputn(const char_type* s, streamsize n);
protected:
basic_streambuf();
//
lib.streambuf.get.area Get area:
char_type* eback() const;
char_type* gptr() const;
char_type* egptr() const;
void gbump(int n);
void setg(char_type* gbeg, char_type* gnext, char_type* gend);
//
lib.streambuf.put.area Put area:
char_type* pbase() const;
char_type* pptr() const;
char_type* epptr() const;
void pbump(int n);
void setp(char_type* pbeg, char_type* pend);
//
lib.streambuf.virtuals virtual functions:
// lib.streambuf.virt.locales Locales:
virtual void imbue(const locale &loc);
//
lib.streambuf.virt.buffer Buffer management and positioning:
virtual basic_streambuf<char_type,traits>*
setbuf(char_type* s, streamsize n);
virtual pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which = ios_base::in | ios_base::out);
virtual pos_type seekpos(pos_type sp,
ios_base::openmode which = ios_base::in | ios_base::out);
virtual int sync();
//
lib.streambuf.virt.get Get area:
virtual int showmanyc();
virtual streamsize xsgetn(char_type* s, streamsize n);
virtual int_type underflow();
virtual int_type uflow();
//
lib.streambuf.virt.pback Putback:
virtual int_type pbackfail(int_type c = traits::eof());
//
lib.streambuf.virt.put Put area:
virtual streamsize xsputn(const char_type* s, streamsize n);
virtual int_type overflow (int_type c = traits::eof());
};
}
27.5.2.1 - basic_streambuf constructors [lib.streambuf.cons]
basic_streambuf();
[Footnote:
The default constructor is protected for class
basic_streambuf
to assure that only objects for classes
derived from this class may be constructed.
--- end foonote]
27.5.2.2 - basic_streambuf public member functions [lib.streambuf.members]
27.5.2.2.1 - Locales [lib.streambuf.locales]
locale pubimbue(const locale& loc );
locale getloc() const;
27.5.2.2.2 - Buffer management and positioning [lib.streambuf.buffer]
basic_streambuf<char_type,traits>* pubsetbuf(char_type* s, streamsize n);
pos_type pubseekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which = ios_base::in | ios_base::out);
pos_type pubseekpos(pos_type sp,
ios_base::openmode which = ios_base::in | ios_base::out);
int pubsync();
27.5.2.2.3 - Get area [lib.streambuf.pub.get]
streamsize in_avail();
int_type snextc();
int_type sbumpc();
int_type sgetc();
streamsize sgetn(char_type* s, streamsize n);
27.5.2.2.4 - Putback [lib.streambuf.pub.pback]
int_type sputbackc(char_type c);
int_type sungetc();
27.5.2.2.5 - Put area [lib.streambuf.pub.put]
int_type sputc(char_type c);
streamsize sputn(const char_type* s, streamsize n);
27.5.2.3 - basic_streambuf protected member functions [lib.streambuf.protected]
27.5.2.3.1 - Get area access [lib.streambuf.get.area]
char_type* eback() const;
char_type* gptr() const;
char_type* egptr() const;
void gbump(int n);
void setg(char_type* gbeg, char_type* gnext, char_type* gend);
27.5.2.3.2 - Put area access [lib.streambuf.put.area]
char_type* pbase() const;
char_type* pptr() const;
char_type* epptr() const;
void pbump(int n);
void setp(char_type* pbeg, char_type* pend);
27.5.2.4 - basic_streambuf virtual functions [lib.streambuf.virtuals]
27.5.2.4.1 - Locales [lib.streambuf.virt.locales]
void imbue(const locale&)
27.5.2.4.2 - Buffer management and positioning [lib.streambuf.virt.buffer]
basic_streambuf* setbuf(char_type* s, streamsize n);
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
pos_type seekpos(pos_type sp,
ios_base::openmode which = in | out);
int sync();
27.5.2.4.3 - Get area [lib.streambuf.virt.get]
streamsize showmanyc();*
[Footnote:
The morphemes of
showmanyc
are "es-how-many-see", not "show-manic".
--- end foonote]
[Footnote:
underflow
or
uflow
might fail by throwing an exception prematurely.
The intention is not only that the calls will not return
eof()
but that they will return ``immediately.''
--- end foonote]
streamsize xsgetn(char_type* s, streamsize n);
[Footnote:
Classes derived from
basic_streambuf
can provide more efficient ways to implement
xsgetn()
and
xsputn()
by overriding these definitions from the base class.
--- end foonote]
int_type underflow();
int_type uflow();
27.5.2.4.4 - Putback [lib.streambuf.virt.pback]
int_type pbackfail(int_type c = traits::eof());
The
pending sequence
is defined as for
underflow(),
with the modifications that
Returns some value other than
traits::eof()
to indicate success.
27.5.2.4.5 - Put area [lib.streambuf.virt.put]
streamsize xsputn(const char_type* s, streamsize n);
int_type overflow(int_type c = traits::eof());
[Footnote:
That is, for each class derived from an instance of
basic_streambuf
in this clause
(lib.stringbuf,
lib.filebuf),
a specification of how consuming a character effects the associated output sequence is given.
There is no requirement on a program-defined class.
--- end foonote]
Otherwise,
returns some value other than
traits::eof()
to indicate success.*
[Footnote:
Typically,
overflow
returns c to indicate success, except when
traits::eq_int_type(c,traits::eof())
returns true, in which case it returns
traits::not_eof(c).
--- end foonote]
27.6 - Formatting and manipulators [lib.iostream.format]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_istream;
typedef basic_istream<char> istream;
typedef basic_istream<wchar_t> wistream;
template <class charT, class traits = char_traits<charT> >
class basic_iostream;
typedef basic_iostream<char> iostream;
typedef basic_iostream<wchar_t> wiostream;
template <class charT, class traits>
basic_istream<charT,traits>& ws(basic_istream<charT,traits>& is);
}
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ostream;
typedef basic_ostream<char> ostream;
typedef basic_ostream<wchar_t> wostream;
template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
template <class charT, class traits>
basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
template <class charT, class traits>
basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
}
namespace std {
//
Types T1, T2, ... are unspecified implementation types
T1 resetiosflags(ios_base::fmtflags mask);
T2 setiosflags (ios_base::fmtflags mask);
T3 setbase(int base);
template<charT> T4 setfill(charT c);
T5 setprecision(int n);
T6 setw(int n);
}
27.6.1 - Input streams [lib.input.streams]
27.6.1.1 - Template class basic_istream [lib.istream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_istream : virtual public basic_ios<charT,traits> {
public:
//
Types (inherited from basic_ios (lib.ios)):
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
//
lib.istream.cons Constructor/destructor:
explicit basic_istream(basic_streambuf<charT,traits>* sb);
virtual ~basic_istream();
//
lib.istream::sentry Prefix/suffix:
class sentry;
//
lib.istream.formatted Formatted input:
basic_istream<charT,traits>& operator>>
(basic_istream<charT,traits>& (*pf)(basic_istream<charT,traits>&))
basic_istream<charT,traits>& operator>>
(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&))
basic_istream<charT,traits>& operator>>
(ios_base& (*pf)(ios_base&))
basic_istream<charT,traits>& operator>>(bool& n);
basic_istream<charT,traits>& operator>>(short& n);
basic_istream<charT,traits>& operator>>(unsigned short& n);
basic_istream<charT,traits>& operator>>(int& n);
basic_istream<charT,traits>& operator>>(unsigned int& n);
basic_istream<charT,traits>& operator>>(long& n);
basic_istream<charT,traits>& operator>>(unsigned long& n);
basic_istream<charT,traits>& operator>>(float& f);
basic_istream<charT,traits>& operator>>(double& f);
basic_istream<charT,traits>& operator>>(long double& f);
basic_istream<charT,traits>& operator>>(void*& p);
basic_istream<charT,traits>& operator>>
(basic_streambuf<char_type,traits>* sb);
//
lib.istream.unformatted Unformatted input:
streamsize gcount() const;
int_type get();
basic_istream<charT,traits>& get(char_type& c);
basic_istream<charT,traits>& get(char_type* s, streamsize n);
basic_istream<charT,traits>& get(char_type* s, streamsize n,
char_type delim);
basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb);
basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb,
char_type delim);
basic_istream<charT,traits>& getline(char_type* s, streamsize n);
basic_istream<charT,traits>& getline(char_type* s, streamsize n,
char_type delim);
basic_istream<charT,traits>& ignore
(streamsize n = 1, int_type delim = traits::eof());
int_type peek();
basic_istream<charT,traits>& read (char_type* s, streamsize n);
streamsize readsome(char_type* s, streamsize n);
basic_istream<charT,traits>& putback(char_type c);
basic_istream<charT,traits>& unget();
int sync();
pos_type tellg();
basic_istream<charT,traits>& seekg(pos_type);
basic_istream<charT,traits>& seekg(off_type, ios_base::seekdir);
};
//
lib.istream::extractors character extraction templates:
template<class charT, class traits>
basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&,
charT&);
template<class traits>
basic_istream<char,traits>& operator>>(basic_istream<char,traits>&,
unsigned char&);
template<class traits>
basic_istream<char,traits>& operator>>(basic_istream<char,traits>&,
signed char&);
template<class charT, class traits>
basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&,
charT*);
template<class traits>
basic_istream<char,traits>& operator>>(basic_istream<char,traits>&,
unsigned char*);
template<class traits>
basic_istream<char,traits>& operator>>(basic_istream<char,traits>&,
signed char*);
}
27.6.1.1.1 - basic_istream constructors [lib.istream.cons]
explicit basic_istream(basic_streambuf<charT,traits>* sb);
virtual ~basic_istream();
27.6.1.1.2 - Class basic_istream::sentry [lib.istream::sentry]
namespace std {
template <class charT,class traits = char_traits<charT> >
class basic_istream<charT,traits>::sentry {
typedef traits traits_type;
bool ok_; // exposition only
public:
explicit sentry(basic_istream<charT,traits>& is, bool noskipws = false);
~sentry();
operator bool() const { return ok_; }
private:
sentry(const sentry&); // not defined
sentry& operator=(const sentry&); // not defined
};
}
explicit sentry(basic_istream<charT,traits>& is, bool noskipws = false);
[Footnote:
This will be possible only in functions that are part of the library.
The semantics of the constructor used in user code is as specified.
--- end foonote]
If noskipws is zero and
is.flags() & ios_base::skipws
is nonzero, the function extracts and discards each character as long as
the next available input character c is a whitespace character.
const ctype<charT>& ctype = use_facet<ctype<charT> >(is.getloc());
if (ctype.is(ctype.space,c)!=0)
//
c is a whitespace character.
[Footnote:
The sentry constructor and destructor
can also perform additional implementation-dependent operations.
--- end foonote]
template <class charT, class traits = char_traits<charT> >
basic_istream<charT,traits>::sentry(
basic_istream<charT,traits>& is, bool noskipws = false) {
...
int_type c;
typedef ctype<charT> ctype_type;
const ctype_type& ctype = use_facet<ctype_type>(is.getloc());
while ((c = is.rdbuf()->snextc()) != traits::eof()) {
if (ctype.is(ctype.space,c)==0) {
is.rdbuf()->sputbackc (c);
break;
}
}
...
}
--- end example]
~sentry();
operator bool() const;
27.6.1.2 - Formatted input functions [lib.istream.formatted]
27.6.1.2.1 - Common requirements [lib.istream.formatted.reqmts]
[Footnote:
This is done without causing an
ios::failure
to be thrown.
--- end foonote]
If
(exception()&badbit)!= 0
then the exception is rethrown.
In any case, the formatted input function destroys the
sentry
object.
If no exception has been thrown, it returns
*this.
27.6.1.2.2 - Arithmetic Extractors [lib.istream.formatted.arithmetic]
As in the case of the inserters, these extractors depend on the
locale's
num_get<>
(lib.locale.num.get) object to perform parsing the input
stream data.
The conversion occurs as if performed by the following code fragment:
operator>>(short& val);
operator>>(unsigned short& val);
operator>>(int& val);
operator>>(unsigned int& val);
operator>>(long& val);
operator>>(unsigned long& val);
operator>>(float& val);
operator>>(double& val);
operator>>(long double& val);
operator>>(bool& val);
operator>>(void*& val);
In the above fragment,
loc
stands for the private member of the
basic_ios
class.
[Note:
The first argument provides an object of the
istream_iterator
class which is an iterator pointed to an input stream.
It bypasses istreams and uses streambufs directly.
typedef num_get< charT,istreambuf_iterator<charT,traits> > numget;
iostate err = 0;
use_facet< numget >(loc).get(*this, 0, *this, err, val);
setstate(err);
--- end note]
Class
locale
relies on this
type as its interface to
istream,
so that it does not need to depend directly on
istream.
27.6.1.2.3 - basic_istream::operator>> [lib.istream::extractors]
basic_istream<charT,traits>& operator>>
(basic_istream<charT,traits>& (*pf)(basic_istream<charT,traits>&))
[Footnote:
See, for example, the function signature
ws(basic_istream&)
(lib.istream.manip).
--- end foonote]
basic_istream<charT,traits>& operator>>
(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&));
basic_istream<charT,traits>& operator>>
(ios_base& (*pf)(ios_base&));
[Footnote:
See, for example, the function signature
dec(ios_base&)
(lib.basefield.manip).
--- end foonote]
template<class charT, class traits>
basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>& in,
charT* s);
template<class traits>
basic_istream<char,traits>& operator>>(basic_istream<char,traits>& in,
unsigned char* s);
template<class traits>
basic_istream<char,traits>& operator>>(basic_istream<char,traits>& in,
signed char* s);
operator>>
then calls
width(0).
template<class charT, class traits>
basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>& in,
charT& c);
template<class traits>
basic_istream<char,traits>& operator>>(basic_istream<char,traits>& in,
unsigned char& c);
template<class traits>
basic_istream<char,traits>& operator>>(basic_istream<char,traits>& in,
signed char& c);
basic_istream<charT,traits>& operator>>
(basic_streambuf<charT,traits>* sb);
Extracts characters from
*this
and inserts them in the output sequence controlled by sb.
Characters are extracted and inserted until any of the following occurs:
27.6.1.3 - Unformatted input functions [lib.istream.unformatted]
[Footnote:
This is done without causing an
ios::failure
to be thrown.
--- end foonote]
If
(exception()&badbit)!= 0
then the exception is rethrown.
It also counts the number of characters extracted.
If no exception has been thrown it ends
by storing the count in a member object
and returning the value specified.
In any event the
sentry
object
is destroyed before leaving the unformatted input function.
streamsize gcount() const;
int_type get();
basic_istream<charT,traits>& get(char_type& c);
[Footnote:
Note that this function is not overloaded on types
signed charand
unsigned char.
--- end foonote]
Otherwise, the function calls
setstate(failbit)
(which may throw
ios_base::failure
(lib.iostate.flags)).
basic_istream<charT,traits>& get(char_type* s, streamsize n,
char_type delim );
[Footnote:
Note that this function is not overloaded on types
signed char
and
unsigned char.
--- end foonote]
Characters are extracted and stored until any of the following occurs:
basic_istream<charT,traits>& get(char_type* s, streamsize n)
basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb,
char_type delim );
basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb);
basic_istream<charT,traits>& getline(char_type* s, streamsize n,
char_type delim);
[Footnote:
Note that this function is not overloaded on types
signed char
and
unsigned char.
--- end foonote]
Characters are extracted and stored until one of the following occurs:
[Footnote:
Since the final input character is ``extracted,''
it is counted in the
gcount(),
even though it is not stored.
--- end foonote]
[Footnote:
This allows an input line which exactly fills the buffer, without setting
failbit.
This is different behavior than the historical AT&T implementation.
--- end foonote]
[Footnote:
This implies an empty input line will not cause
failbit
to be set.
--- end foonote]
#include <iostream>
int main()
{
using namespace std;
const int line_buffer_size = 100;
char buffer[line_buffer_size];
int line_number = 0;
while (cin.getline(buffer, line_buffer_size, '\n') || cin.gcount()) {
int count = cin.gcount();
if (cin.eof())
cout << "Partial final line"; //
cin.fail() is false
else if (cin.fail()) {
cout << "Partial long line";
cin.clear(cin.rdstate() & ~ios::failbit);
} else {
count--; // Don't include newline in count
cout << "Line " << ++line_number;
}
cout << " (" << count << " chars): " << buffer << endl;
}
}
--- end example]
basic_istream<charT,traits>& getline(char_type* s, streamsize n);
basic_istream<charT,traits>&
ignore(int n = 1, int_type delim = traits::eof());
int_type peek();
basic_istream<charT,traits>& read(char_type* s, streamsize n);
[Footnote:
Note that this function is not overloaded on types
signed char
and
unsigned char.
--- end foonote]
Characters are extracted and stored until either of the following occurs:
streamsize readsome(char_type* s, streamsize n);
basic_istream<charT,traits>& putback(char_type c);
basic_istream<charT,traits>& unget();
int sync();
pos_type tellg();
basic_istream<charT,traits>& seekg(pos_type pos);
basic_istream<charT,traits>& seekg(off_type& off, ios_base::seekdir dir);
27.6.1.4 - Standard basic_istream manipulators [lib.istream.manip]
namespace std {
template <class charT, class traits>
basic_istream<charT,traits>& ws(basic_istream<charT,traits>& is);
}
27.6.1.5 - Template class basic_iostream [lib.iostreamclass]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_iostream :
public basic_istream<charT,traits>,
public basic_ostream<charT,traits> {
public:
//
constructor/destructor
explicit basic_iostream(basic_streambuf<charT,traits>* sb);
virtual ~basic_iostream();
};
}
27.6.1.5.1 - basic_iostream constructors [lib.iostream.cons]
explicit basic_iostream(basic_streambuf<charT,traits>* sb);
27.6.1.5.2 - basic_iostream destructor [lib.iostream.dest]
virtual ~basic_iostream();
27.6.2 - Output streams [lib.output.streams]
27.6.2.1 - Template class basic_ostream [lib.ostream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ostream : virtual public basic_ios<charT,traits> {
public:
//
Types (inherited from basic_ios (lib.ios)):
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
//
lib.ostream.cons Constructor/destructor:
explicit basic_ostream(basic_streambuf<char_type,traits>* sb);
virtual ~basic_ostream();
//
lib.ostream::sentry Prefix/suffix:
class sentry;
//
lib.ostream.formatted Formatted output:
basic_ostream<charT,traits>& operator<<
(basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&));
basic_ostream<charT,traits>& operator<<
(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&));
basic_ostream<charT,traits>& operator<<
(ios_base& (*pf)(ios_base&));
basic_ostream<charT,traits>& operator<<(bool n);
basic_ostream<charT,traits>& operator<<(short n);
basic_ostream<charT,traits>& operator<<(unsigned short n);
basic_ostream<charT,traits>& operator<<(int n);
basic_ostream<charT,traits>& operator<<(unsigned int n);
basic_ostream<charT,traits>& operator<<(long n);
basic_ostream<charT,traits>& operator<<(unsigned long n);
basic_ostream<charT,traits>& operator<<(float f);
basic_ostream<charT,traits>& operator<<(double f);
basic_ostream<charT,traits>& operator<<(long double f);
basic_ostream<charT,traits>& operator<<(const void* p);
basic_ostream<charT,traits>& operator<<
(basic_streambuf<char_type,traits>* sb);
//
lib.ostream.unformatted Unformatted output:
basic_ostream<charT,traits>& put(char_type c);
basic_ostream<charT,traits>& write(const char_type* s, streamsize n);
basic_ostream<charT,traits>& flush();
//
lib.ostream.seeks seeks:
pos_type tellp();
basic_ostream<charT,traits>& seekp(pos_type);
basic_ostream<charT,traits>& seekp(off_type, ios_base::seekdir);
};
//
lib.ostream.inserters.character character inserters
template<class charT, class traits>
basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&,
charT);
template<class charT, class traits>
basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&,
char);
// specialization
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
char);
// signed and unsigned
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
signed char);
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
unsigned char)
template<class charT, class traits>
basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&,
const charT*);
template<class charT, class traits>
basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&,
const char*);
//
partial specializationss
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
const char*);
// signed and unsigned
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
const signed char*);
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&,
const unsigned char*);
}
27.6.2.2 - basic_ostream constructors [lib.ostream.cons]
explicit basic_ostream(basic_streambuf<charT,traits>* sb);
virtual ~basic_ostream();
27.6.2.3 - Class basic_ostream::sentry [lib.ostream::sentry]
namespace std {
template <class charT,class traits = char_traits<charT> >
class basic_ostream<charT,traits>::sentry {
bool ok_; // exposition only
public:
explicit sentry(basic_ostream<charT,traits>& os);
~sentry();
operator bool() const { return ok_; }
private
sentry(const sentry&); // not defined
sentry& operator=(const sentry&); // not defined
};
}
explicit sentry(basic_ostream<charT,traits>& os);
[Footnote:
The call
os.tie()->flush()
does not necessarily occur if the function can determine that no
synchronization is necessary.
--- end foonote]
[Footnote:
The
sentry
constructor and destructor
can also perform additional
implementation-dependent operations.
--- end foonote]
~sentry();
operator bool();
27.6.2.4 - basic_ostream seek members [lib.ostream.seeks]
pos_type tellp();
basic_ostream<charT,traits>& seekp(pos_type& pos);
basic_ostream<charT,traits>& seekp(off_type& off, ios_base::seekdir dir);
27.6.2.5 - Formatted output functions [lib.ostream.formatted]
27.6.2.5.1 - Common requirements [lib.ostream.formatted.reqmts]
[Footnote:
without causing an
ios::failure
to be thrown.
--- end foonote]
If
(exception()&badbit) != 0
then the exception is rethrown.
Whether or not an exception is thrown, the
sentry
object is destroyed before leaving the formatted output function.
If no exception is thrown, the result of the formattted output function
is
*this.
27.6.2.5.2 - Arithmetic Inserters [lib.ostream.inserters.arithmetic]
operator<<(bool val);
operator<<(short val);
operator<<(unsigned short val);
operator<<(int val);
operator<<(unsigned int val);
operator<<(long val);
operator<<(unsigned long val);
operator<<(float val);
operator<<(double val);
operator<<(long double val);
operator<<(const void* val);
The first argument provides an object of the
ostreambuf_iterator<>
class which is an iterator for class basic_ostream<>.
It bypasses
ostreams
and uses
streambufs
directly.
Class
locale
relies on these types as its
interface to iostreams, since for flexibility it has been abstracted
away from direct dependence on
ostream.
The second parameter is a reference to the base subobject of type
ios_base.
It provides formatting specifications such as field width, and
a locale from which to obtain other facets.
If
failed
is true then does
setstate(badbit),
which may throw an exception, and returns.
bool failed =
use_facet< num_put<charT,ostreambuf_iterator<charT,traits> > >(getloc()).
put(*this, *this, fill(), val). failed();
27.6.2.5.3 - basic_ostream::operator<< [lib.ostream.inserters]
basic_ostream<charT,traits>& operator<<
(basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&))
[Footnote:
See, for example, the function signature
endl(basic_ostream&)
(lib.ostream.manip) .
--- end foonote]
basic_ostream<charT,traits>& operator<<
(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&))
[Footnote:
See, for example, the function signature
dec(ios_base&)
(lib.basefield.manip).
--- end foonote]
basic_ostream<charT,traits>& operator<<
(ios_base& (*pf)(ios_base&))
basic_ostream<charT,traits>& operator<<
(basic_streambuf<charT,traits>* sb);
27.6.2.5.4 - Character inserter template functions [lib.ostream.inserters.character]
template<class charT, class traits>
basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>& out,
charT c);
template<class charT, class traits>
basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>& out,
char c);
// specialization
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out,
char c);
// signed and unsigned
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out,
signed char c);
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out,
unsigned char c);
[Footnote:
In case the insertion is into a
char
stream,
widen(c)
will usually be c.
--- end foonote]
Padding is determined as described in lib.facet.num.put.virtuals.
width(0)
is called.
The insertion character and any required padding are inserted into out.
template<class charT, class traits>
basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>& out,
const charT* s);
template<class charT, class traits>
basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>& out,
const char* s);
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out,
const char* s);
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out,
const signed char* s);
template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out,
const unsigned char* s);
27.6.2.6 - Unformatted output functions [lib.ostream.unformatted]
basic_ostream<charT,traits>& put(char_type c);
[Footnote:
Note that this function is not overloaded on types
signed char
and
unsigned char.
--- end foonote]
basic_ostream& write(const char_type* s, streamsize n);
[Footnote:
Note that this function is not overloaded on types
signed char
and
unsigned char.
--- end foonote]
Characters are inserted until either of the following occurs:
basic_ostream& flush();
27.6.2.7 - Standard basic_ostream manipulators [lib.ostream.manip]
namespace std {
template <class charT, class traits>
basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
}
[Footnote:
The effect of executing
cout << endl
is to insert a newline character in the output sequence controlled by
cout,
then synchronize it with any external file
with which it might be associated.
--- end foonote]
namespace std {
template <class charT, class traits>
basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
}
namespace std {
template <class charT, class traits>
basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
}
27.6.3 - Standard manipulators [lib.std.manip]
smanip resetiosflags(ios_base::fmtflags mask);
[Footnote:
The expression
cin >> resetiosflags(ios_base::skipws)
clears
ios_base::skipws
in the format flags stored in the
istream
object
cin
(the same as
cin >> noskipws),
and the expression
cout << resetiosflags(ios_base::showbase)
clears
ios_base::showbase
in the format flags stored in the
ostream
object
cout
(the same as
cout << noshowbase).
--- end foonote]
The expression
out<<s
has type
ostream&
and value
out.
The expression
in>>s
has type
istream&
and value
in.
ios_base& f(ios_base& str, ios_base::fmtflags mask)
{
//
reset specified flags
str.setf(ios_base::fmtflags(0), mask);
return str;
}
smanip setiosflags(ios_base::fmtflags mask);
The expression
out<<s
has type
ostream&
and value
out.
The expression
in>>s
has type
istream&
and value
in.
ios_base& f(ios_base& str, ios_base::fmtflags mask)
{
//
set specified flags
str.setf(mask);
return str;
}
smanip setbase(int base);
The expression
out<<s
has type
ostream&
and value
out.
The expression
in>>s
has type
istream&
and value
in.
ios_base& f(ios_base& str, int base)
{
//
set basefield
str.setf(n == 8 ? ios_base::oct :
n == 10 ? ios_base::dec :
n == 16 ? ios_base::hex :
ios_base::fmtflags(0), ios_base::basefield);
return str;
}
smanip setfill(char_type c);
The expression
out<<s
has type
ostream&
and value
out.
template<class charT, class traits>
basic_ios<charT,traits>& f(basic_ios<charT,traits>& str, charT c)
{
//
set fill character
str.fill(c);
return str;
}
smanip setprecision(int n);
The expression
out<<s
has type
ostream&
and value
out.
The expression
in>>s
has type
istream&
and value
in.
ios_base& f(ios_base& str, int n)
{
//
set precision
str.precision(n);
return str;
}
smanip setw(int n);
The expression
out<<s
has type
ostream&
and value
out.
The expression
in>>s
has type
istream&
and value
in.
ios_base& f(ios_base& str, int n)
{
//
set width
str.width(n);
return str;
}
27.7 - String-based streams [lib.string.streams]
namespace std {
template <class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_stringbuf;
typedef basic_stringbuf<char> stringbuf;
typedef basic_stringbuf<wchar_t> wstringbuf;
template <class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_istringstream;
typedef basic_istringstream<char> istringstream;
typedef basic_istringstream<wchar_t> wistringstream;
template <class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_ostringstream;
typedef basic_ostringstream<char> ostringstream;
typedef basic_ostringstream<wchar_t> wostringstream;
template <class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_stringstream;
typedef basic_stringstream<char> stringstream;
typedef basic_stringstream<wchar_t> wstringstream;
}
27.7.1 - Template class basic_stringbuf [lib.stringbuf]
namespace std {
template <class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_stringbuf : public basic_streambuf<charT,traits> {
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
//
lib.stringbuf.cons Constructors:
explicit basic_stringbuf(ios_base::openmode which
= ios_base::in | ios_base::out);
explicit basic_stringbuf
(const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::in | ios_base::out);
//
lib.stringbuf.members Get and set:
basic_string<charT,traits,Allocator> str() const;
void str(const basic_string<charT,traits,Allocator>& s);
protected:
//
lib.stringbuf.virtuals Overridden virtual functions:
virtual int_type underflow();
virtual int_type pbackfail(int_type c = traits::eof());
virtual int_type overflow (int_type c = traits::eof());
virtual basic_streambuf<charT,traits>* setbuf(charT*, streamsize);
virtual pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
virtual pos_type seekpos(pos_type sp,
ios_base::openmode which
= ios_base::in | ios_base::out);
private:
// ios_base::openmode mode;
exposition only
};
}
27.7.1.1 - basic_stringbuf constructors [lib.stringbuf.cons]
explicit basic_stringbuf(ios_base::openmode which =
ios_base::in | ios_base::out);
explicit basic_stringbuf(const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::in | ios_base::out);
27.7.1.2 - Member functions [lib.stringbuf.members]
basic_string<charT,traits,Allocator> str() const;
void str(const basic_string<charT,traits,Allocator>& s);
27.7.1.3 - Overridden virtual functions [lib.stringbuf.virtuals]
int_type underflow();
Otherwise, returns
traits::eof().
int_type pbackfail(int_type c = traits::eof());
Returns:
c .
Returns:
c.
Returns:
traits::not_eof(c).
int_type overflow(int_type c = traits::eof());
Signals success by returning c .
Signals success by returning a value other than
traits::eof().
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
Conditions Result (which & basic_ios::in) != 0 positions the input sequence (which & basic_ios::out) != 0 positions the output sequence
(which & (basic_ios::in | basic_ios::out)) ==
(basic_ios::in | basic_ios::out))
and
way == either basic_ios::beg or basic_ios::end
positions both the input and the output sequences Otherwise the positioning operation fails.
Condition newoff Value way == basic_ios::beg 0 way == basic_ios::cur
the next pointer minus the beginning pointer
( xnext - xbeg ).
way == basic_ios::end
the end pointer minus the beginning pointer
( xend - xbeg )
pos_type seekpos(pos_type sp, ios_base::openmode which
= ios_base::in | ios_base::out);
basic_streambuf<charT,traits>* setbuf(charT* s, streamsize n);
27.7.2 - Template class basic_istringstream [lib.istringstream]
namespace std {
template <class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_istringstream : public basic_istream<charT,traits> {
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
//
lib.istringstream.cons Constructors:
explicit basic_istringstream(ios_base::openmode which = ios_base::in);
explicit basic_istringstream(
const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::in);
//
lib.istringstream.members Members:
basic_stringbuf<charT,traits,Allocator>* rdbuf() const;
basic_string<charT,traits,Allocator> str() const;
void str(const basic_string<charT,traits,Allocator>& s);
private:
// basic_stringbuf<charT,traits,Allocator> sb; exposition only
};
}
27.7.2.1 - basic_istringstream constructors [lib.istringstream.cons]
explicit basic_istringstream(ios_base::openmode which = ios_base::in);
explicit basic_istringstream(
const basic_string<charT,traits,allocator>& str,
ios_base::openmode which = ios_base::in);
27.7.2.2 - Member functions [lib.istringstream.members]
basic_stringbuf<charT,traits,Allocator>* rdbuf() const;
basic_string<charT,traits,Allocator> str() const;
[Footnote:
rdbuf()
is never NULL because it always returns the private
object.
--- end foonote]
void str(const basic_string<charT,traits,Allocator>& s);
27.7.3 - Class basic_ostringstream [lib.ostringstream]
namespace std {
template <class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_ostringstream : public basic_ostream<charT,traits> {
public:
//
Types:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
//
lib.ostringstream.cons Constructors/destructor:
explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
explicit basic_ostringstream(
const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::out);
//
lib.ostringstream.members Members:
basic_stringbuf<charT,traits,Allocator>* rdbuf() const;
basic_string<charT,traits,Allocator> str() const;
void str(const basic_string<charT,traits,Allocator>& s);
private:
// basic_stringbuf<charT,traits,Allocator> sb; exposition only
};
}
27.7.3.1 - basic_ostringstream constructors [lib.ostringstream.cons]
explicit basic_ostringstream(ios_base::openmode which = ios_base::out);
explicit basic_ostringstream(
const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::out);
27.7.3.2 - Member functions [lib.ostringstream.members]
basic_stringbuf<charT,traits,Allocator>* rdbuf() const;
basic_string<charT,traits,Allocator> str() const;
[Footnote:
rdbuf()
is never NULL because it always returns the private
object.
--- end foonote]
void str(const basic_string<charT,traits,Allocator>& s);
27.7.4 - Template class basic_stringstream [lib.stringstream]
namespace std {
template <class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_stringstream
: public basic_iostream<charT,traits> {
public:
//
Types
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
//
constructors/destructors
explicit basic_stringstream(
ios_base::openmode which = ios_base::out|ios_base::in);
explicit basic_stringstream(
const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::out|ios_base::in);
//
Members:
basic_stringbuf<charT,traits,Allocator>* rdbuf() const;
basic_string<charT,traits,Allocator> str() const;
void str(const basic_string<charT,traits,Allocator>& str);
private:
// basic_stringbuf<charT, traits> sb ; exposition only
};
}
27.7.5 - basic_stringstream constructors [lib.stringstream.cons]
explicit basic_stringstream(
ios_base::openmode which = ios_base::out|iosbase::in);
explicit basic_stringstream(
const basic_string<charT,traits,Allocator>& str,
ios_base::openmode which = ios_base::out|iosbase::in);
27.7.6 - Member functions [lib.stringstream.members]
basic_stringbuf<charT,traits,Allocator>* rdbuf() const;
basic_string<charT,traits,Allocator> str() const;
[Footnote:
rdbuf()
is never NULL because it always returns the private
object.
--- end foonote]
void str(const basic_string<charT,traits,Allocator>& str);
27.8 - File-based streams [lib.file.streams]
27.8.1 - File streams [lib.fstreams]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_filebuf;
typedef basic_filebuf<char> filebuf;
typedef basic_filebuf<wchar_t> wfilebuf;
template <class charT, class traits = char_traits<charT> >
class basic_ifstream;
typedef basic_ifstream<char> ifstream;
typedef basic_ifstream<wchar_t> wifstream;
template <class charT, class traits = char_traits<charT> >
class basic_ofstream;
typedef basic_ofstream<char> ofstream;
typedef basic_ofstream<wchar_t> wofstream;
template <class charT, class traits = char_traits<charT> >
class basic_fstream;
typedef basic_fstream<char> fstream;
typedef basic_fstream<wchar_t> wfstream;
}
[Footnote:
In C
FILE
must be a
typedef.
In C++ it may be a
typedef
or other type name.
--- end foonote]
[Footnote:
A File is a sequence of multibyte characters.
In order to provide the
contents as a wide character sequence,
filebuf
should convert between
wide character sequences and multibyte character sequences.
--- end foonote]
27.8.1.1 - Template class basic_filebuf [lib.filebuf]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_filebuf : public basic_streambuf<charT,traits> {
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
//
lib.filebuf.cons Constructors/destructor:
basic_filebuf();
virtual ~basic_filebuf();
//
lib.filebuf.members Members:
bool is_open() const;
basic_filebuf<charT,traits>* open
(const char* s, ios_base::openmode mode);
basic_filebuf<charT,traits>* close();
protected:
//
lib.filebuf.virtuals Overridden virtual functions:
virtual streamsize showmanyc();
virtual int_type underflow();
virtual int_type uflow();
virtual int_type pbackfail(int_type c = traits::eof());
virtual int_type overflow (int_type c = traits::eof());
virtual basic_streambuf<charT,traits>*
setbuf(char_type* s, streamsize n);
virtual pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode which
= ios_base::in | ios_base::out);
virtual pos_type seekpos(pos_type sp, ios_base::openmode which
= ios_base::in | ios_base::out);
virtual int sync();
virtual void imbue(const locale& loc);
};
}
codecvt<charT,char,typename traits::state_type> a_codecvt =
use_facet<codecvt<charT,char,typename traits::state_type> >(getloc());
27.8.1.2 - basic_filebuf constructors [lib.filebuf.cons]
basic_filebuf();
virtual ~basic_filebuf();
27.8.1.3 - Member functions [lib.filebuf.members]
bool is_open() const;
basic_filebuf<charT,traits>* open(
const char* s,
ios_base::openmode mode );
It then opens a file, if possible, whose name is the
NTBS s
(``as if'' by calling
std::fopen(s,modstr)).
The NTBS modstr is determined from
mode & ~ios_base::ate
as indicated in Table ??:
ios_base Flag combination stdio equivalent binary in out trunc app + "w" + + "a" + + "w" + "r" + + "r+" + + + "w+" + + "wb" + + + "ab" + + + "wb" + + "rb" + + + "r+b" + + + + "w+b"
[Footnote:
The macro
SEEK_END
is defined, and the function signatures
fopen(const char_type*, const char_type*)
and
fseek(FILE*, long, int)
are declared, in
<cstdio>
(lib.c.files).
--- end foonote]
basic_filebuf<charT,traits>* close();
[Footnote:
The function signature
fclose(FILE*)
is declared in
<cstdio>
(lib.c.files).
--- end foonote]
If any of the calls to
overflow
or
std::fclose
fails then
close
fails.
27.8.1.4 - Overridden virtual functions [lib.filebuf.virtuals]
streamsize showmanyc();
int_type underflow();
This must be done in such a way that the class can recover the
position
(fpos_t)
corresponding to each character between
intern_buf
and
intern_end.
If the value of
r
indicates that
a_codecvt.in()
ran out of space in
intern_buf,
retry with a larger
intern_buf.
char extern_buf[XSIZE];
char* extern_end;
charT intern_buf[ISIZE];
charT* intern_end;
codecvt_base::result r =
a_codecvt.in(st, extern_buf, extern_buf+XSIZE, extern_end,
intern_buf, intern_buf+ISIZE, intern_end);
int_type uflow();
int_type pbackfail(int_type c = traits::eof());
Returns:
c .
Returns:
c .
Returns:
traits::not_eof( c ).
The function does not put back a character directly to the input sequence.
If the function can succeed in more than one of these ways, it is
unspecified which way is chosen.
The function can alter the number of putback positions available as a result of any call.
int_type overflow(int_type c = traits::eof());
and then
charT* b = pbase();
charT* p = pptr();
charT* end;
char xbuf[XSIZE];
char* xbuf_end;
codecvt_base::result r =
a_codecvt.out(st, b, p, end, xbuf, xbuf+XSIZE, xbuf_end);
basic_streambuf* setbuf(char_type* s, int n);
pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode
= ios_base::in | ios_base::out);
way Value stdio Equivalent basic_ios::beg SEEK_SET basic_ios::cur SEEK_CUR basic_ios::end SEEK_END
Alters the file position, if possible, to correspond to the position
stored in sp (as described below).
pos_type seekpos(pos_type sp, ios_base::openmode
= ios_base::in | ios_base::out);
int sync();
void imbue(const locale& loc);
27.8.1.5 - Template class basic_ifstream [lib.ifstream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ifstream : public basic_istream<charT,traits> {
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
//
lib.ifstream.cons Constructors:
basic_ifstream();
explicit basic_ifstream(const char* s,
ios_base::openmode mode = ios_base::in);
//
lib.ifstream.members Members:
basic_filebuf<charT,traits>* rdbuf() const;
bool is_open();
void open(const char* s, ios_base::openmode mode = ios_base::in);
void close();
private:
// basic_filebuf<charT,traits> sb; exposition only
};
}
27.8.1.6 - basic_ifstream constructors [lib.ifstream.cons]
basic_ifstream();
explicit basic_ifstream
(const char* s, ios_base::openmode mode = ios_base::in);
[Footnote:
rdbuf()
is never NULL because it always returns the private
object.
--- end foonote]
If that function returns a null pointer, calls
setstate(failbit),
(which may throw
ios_base::failure).
27.8.1.7 - Member functions [lib.ifstream.members]
basic_filebuf<charT,traits>* rdbuf() const;
bool is_open();
[Footnote:
rdbuf()
is never NULL because it always returns the private
object.
--- end foonote]
void open(const char* s, ios_base::openmode mode = ios_base::in);
void close();
27.8.1.8 - Template class basic_ofstream [lib.ofstream]
namespace std {
template <class charT, class traits = char_traits<charT> >
class basic_ofstream : public basic_ostream<charT,traits> {
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
//
lib.ofstream.cons Constructors:
basic_ofstream();
explicit basic_ofstream(const char* s,
ios_base::openmode mode
= ios_base::out);
//
lib.ofstream.members Members:
basic_filebuf<charT,traits>* rdbuf() const;
bool is_open();
void open(const char* s, ios_base::openmode mode = ios_base::out);
void close();
private:
// basic_filebuf<charT,traits> sb; exposition only
};
}
27.8.1.9 - basic_ofstream constructors [lib.ofstream.cons]
basic_ofstream();
explicit basic_ofstream
(const char* s, ios_base::openmode mode = ios_base::out);
[Footnote:
rdbuf()
is never NULL because it always returns the private
filebuf
object.
--- end foonote]
If that function returns a null pointer, calls
setstate(failbit),
(which may throw
ios_base::failure).
27.8.1.10 - Member functions [lib.ofstream.members]
basic_filebuf<charT,traits>* rdbuf() const;
bool is_open();
void open(const char* s, ios_base::openmode mode = ios_base::out);
void close();
27.8.1.11 - Template class basic_fstream [lib.fstream]
namespace std {
template <class charT, class traits=char_traits<charT> >
class basic_fstream
: public basic_iostream<charT,traits> {
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef traits traits_type;
//
constructors/destructor
basic_fstream();
explicit basic_fstream(
const char* s,
ios_base::openmode mode = ios_base::in|ios_base::out);
//
Members:
basic_filebuf<charT,traits>* rdbuf() const;
bool is_open();
void open(
const char* s,
ios_base::openmode mode = ios_base::in|ios_base::out);
void close();
private:
// basic_filebuf<charT,traits> sb; exposition only
};
}
27.8.1.12 - basic_fstream constructors [lib.fstream.cons]
basic_fstream();
explicit basic_fstream(const char* s, ios_base::openmode mode);
27.8.1.13 - Member functions [lib.fstream.members]
basic_filebuf<charT,traits>* rdbuf() const;
bool is_open();
void open(const char* s, ios_base::openmode mode);
void close();
27.8.2 - C Library files [lib.c.files]
Type Name(s) Macros: BUFSIZ FOPEN_MAX SEEK_CUR TMP_MAX _IONBF stdout EOF L_tmpnam SEEK_END _IOFBF stderr FILENAME_MAX NULL <cstdio> SEEK_SET _IOLBF stdin Types: FILE fpos_t size_t <cstdio> Functions: clearerr fgets fscanf gets rename tmpfile fclose fopen fseek perror rewind tmpnam feof fprintf fsetpos printf scanf ungetc ferror fputc ftell putc setbuf vfprintf fflush fputs fwrite putchar setvbuf vprintf fgetc fread getc puts sprintf vsprintf fgetpos freopen getchar remove sscanf