libstdc++
|
All the operator>>
functions (aka formatted input functions) have some common behavior. Each starts by constructing a temporary object of type std::basic_istream::sentry with the second argument (noskipws) set to false. This has several effects, concluding with the setting of a status flag; see the sentry documentation for more.
If the sentry status is good, the function tries to extract whatever data is appropriate for the type of the argument.
If an exception is thrown during extraction, ios_base::badbit will be turned on in the stream's error state without causing an ios_base::failure to be thrown. The original exception will then be rethrown.
All the unformatted input functions have some common behavior. Each starts by constructing a temporary object of type std::basic_istream::sentry with the second argument (noskipws) set to true. This has several effects, concluding with the setting of a status flag; see the sentry documentation for more.
If the sentry status is good, the function tries to extract whatever data is appropriate for the type of the argument.
The number of characters extracted is stored for later retrieval by gcount().
If an exception is thrown during extraction, ios_base::badbit will be turned on in the stream's error state without causing an ios_base::failure to be thrown. The original exception will then be rethrown.
Controlling input for files.
This class supports reading from named files, using the inherited functions from std::basic_istream. To control the associated sequence, an instance of std::basic_filebuf is used, which this page refers to as sb
.
std::basic_ifstream::basic_ifstream | ( | ) | [inline] |
std::basic_ifstream::basic_ifstream | ( | const char * | __s, |
ios_base::openmode | __mode = ios_base::in |
||
) | [inline, explicit] |
Create an input file stream.
s | Null terminated string specifying the filename. |
mode | Open file in specified mode (see std::ios_base). |
ios_base::in
is automatically included in mode.
Tip: When using std::string to hold the filename, you must use .c_str() before passing it to this constructor.
Definition at line 460 of file fstream.
References open().
std::basic_ifstream::basic_ifstream | ( | const std::string & | __s, |
ios_base::openmode | __mode = ios_base::in |
||
) | [inline, explicit] |
Create an input file stream.
s | std::string specifying the filename. |
mode | Open file in specified mode (see std::ios_base). |
ios_base::in
is automatically included in mode.
Definition at line 476 of file fstream.
References open().
std::basic_ifstream::~basic_ifstream | ( | ) | [inline] |
void std::basic_ifstream::close | ( | ) | [inline] |
Close the file.
Calls std::basic_filebuf::close()
. If that function fails, failbit
is set in the stream's error state.
Definition at line 569 of file fstream.
References std::basic_filebuf::close(), and std::ios_base::failbit.
streamsize std::basic_istream::gcount | ( | ) | const [inline, inherited] |
int_type std::basic_istream::get | ( | void | ) | [inherited] |
Simple extraction.
Tries to extract a character. If none are available, sets failbit and returns traits::eof().
__istream_type& std::basic_istream::get | ( | char_type & | __c | ) | [inherited] |
Simple extraction.
c | The character in which to store data. |
Tries to extract a character and store it in c. If none are available, sets failbit and returns traits::eof().
__istream_type& std::basic_istream::get | ( | char_type * | __s, |
streamsize | __n, | ||
char_type | __delim | ||
) | [inherited] |
Simple multiple-character extraction.
s | Pointer to an array. |
n | Maximum number of characters to store in s. |
delim | A "stop" character. |
Characters are extracted and stored into s until one of the following happens:
n-1
characters are storedIf no characters are stored, failbit is set in the stream's error state.
In any case, a null character is stored into the next location in the array.
__istream_type& std::basic_istream::get | ( | char_type * | __s, |
streamsize | __n | ||
) | [inline, inherited] |
__istream_type& std::basic_istream::get | ( | __streambuf_type & | __sb, |
char_type | __delim | ||
) | [inherited] |
Extraction into another streambuf.
sb | A streambuf in which to store data. |
delim | A "stop" character. |
Characters are extracted and inserted into sb until one of the following happens:
If no characters are stored, failbit is set in the stream's error state.
__istream_type& std::basic_istream::get | ( | __streambuf_type & | __sb | ) | [inline, inherited] |
__istream_type& std::basic_istream::getline | ( | char_type * | __s, |
streamsize | __n, | ||
char_type | __delim | ||
) | [inherited] |
String extraction.
s | A character array in which to store the data. |
n | Maximum number of characters to extract. |
delim | A "stop" character. |
Extracts and stores characters into s until one of the following happens. Note that these criteria are required to be tested in the order listed here, to allow an input line to exactly fill the s array without setting failbit.
delim
, in which case the character is extracted (and therefore counted in gcount()
) but not storedn-1
characters are stored, in which case failbit is set in the stream error stateIf no characters are extracted, failbit is set. (An empty line of input should therefore not cause failbit to be set.)
In any case, a null character is stored in the next location in the array.
__istream_type& std::basic_istream::getline | ( | char_type * | __s, |
streamsize | __n | ||
) | [inline, inherited] |
__istream_type& std::basic_istream::ignore | ( | void | ) | [inherited] |
Discarding characters.
n | Number of characters to discard. |
delim | A "stop" character. |
Extracts characters and throws them away until one of the following happens:
!=
std::numeric_limits<int>::max()
, n characters are extractedtraits::eof()
.NB: Provide three overloads, instead of the single function (with defaults) mandated by the Standard: this leads to a better performing implementation, while still conforming to the Standard.
__istream_type& std::basic_istream::ignore | ( | streamsize | __n | ) | [inherited] |
Simple extraction.
Tries to extract a character. If none are available, sets failbit and returns traits::eof().
__istream_type& std::basic_istream::ignore | ( | streamsize | __n, |
int_type | __delim | ||
) | [inherited] |
Simple extraction.
Tries to extract a character. If none are available, sets failbit and returns traits::eof().
bool std::basic_ifstream::is_open | ( | ) | [inline] |
Wrapper to test for an open file.
Definition at line 510 of file fstream.
References std::basic_filebuf::is_open().
void std::basic_ifstream::open | ( | const char * | __s, |
ios_base::openmode | __mode = ios_base::in |
||
) | [inline] |
Opens an external file.
s | The name of the file. |
mode | The open mode flags. |
Calls std::basic_filebuf::open
(s,mode|in). If that function fails, failbit
is set in the stream's error state.
Tip: When using std::string to hold the filename, you must use .c_str() before passing it to this constructor.
Definition at line 531 of file fstream.
References std::basic_filebuf::open(), std::ios_base::in, and std::ios_base::failbit.
Referenced by basic_ifstream().
void std::basic_ifstream::open | ( | const std::string & | __s, |
ios_base::openmode | __mode = ios_base::in |
||
) | [inline] |
Opens an external file.
s | The name of the file. |
mode | The open mode flags. |
Calls std::basic_filebuf::open
(s,mode|in). If that function fails, failbit
is set in the stream's error state.
Definition at line 551 of file fstream.
References std::basic_filebuf::open(), std::ios_base::in, and std::ios_base::failbit.
__istream_type& std::basic_istream::operator>> | ( | __istream_type &(*)(__istream_type &) | __pf | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | __ios_type &(*)(__ios_type &) | __pf | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | ios_base &(*)(ios_base &) | __pf | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | bool & | __n | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | short & | __n | ) | [inherited] |
Basic arithmetic extractors.
A | variable of builtin type. |
*this
if successfulThese functions use the stream's current locale (specifically, the num_get
facet) to parse the input data.
__istream_type& std::basic_istream::operator>> | ( | unsigned short & | __n | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | int & | __n | ) | [inherited] |
Basic arithmetic extractors.
A | variable of builtin type. |
*this
if successfulThese functions use the stream's current locale (specifically, the num_get
facet) to parse the input data.
__istream_type& std::basic_istream::operator>> | ( | unsigned int & | __n | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | long & | __n | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | unsigned long & | __n | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | long long & | __n | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | unsigned long long & | __n | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | float & | __f | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | double & | __f | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | long double & | __f | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | void *& | __p | ) | [inline, inherited] |
__istream_type& std::basic_istream::operator>> | ( | __streambuf_type * | __sb | ) | [inherited] |
Extracting into another streambuf.
sb | A pointer to a streambuf |
This function behaves like one of the basic arithmetic extractors, in that it also constructs a sentry object and has the same error handling behavior.
If sb is NULL, the stream will set failbit in its error state.
Characters are extracted from this stream and inserted into the sb streambuf until one of the following occurs:
If the function inserts no characters, failbit is set.
int_type std::basic_istream::peek | ( | void | ) | [inherited] |
Looking ahead in the stream.
If, after constructing the sentry object, good()
is false, returns traits::eof()
. Otherwise reads but does not extract the next input character.
__istream_type& std::basic_istream::putback | ( | char_type | __c | ) | [inherited] |
Unextracting a single character.
c | The character to push back into the input stream. |
If rdbuf()
is not null, calls rdbuf()->sputbackc(c)
.
If rdbuf()
is null or if sputbackc()
fails, sets badbit in the error state.
gcount()
will return 0, as required by DR 60. __filebuf_type* std::basic_ifstream::rdbuf | ( | ) | const [inline] |
Accessing the underlying buffer.
This hides both signatures of std::basic_ios::rdbuf().
__istream_type& std::basic_istream::read | ( | char_type * | __s, |
streamsize | __n | ||
) | [inherited] |
Extraction without delimiters.
s | A character array. |
n | Maximum number of characters to store. |
If the stream state is good()
, extracts characters and stores them into s until one of the following happens:
failbit|eofbit
.streamsize std::basic_istream::readsome | ( | char_type * | __s, |
streamsize | __n | ||
) | [inherited] |
Extraction until the buffer is exhausted, but no more.
s | A character array. |
n | Maximum number of characters to store. |
Extracts characters and stores them into s depending on the number of characters remaining in the streambuf's buffer, rdbuf()->in_avail()
, called A
here:
A
==
-1
, sets eofbit and extracts no charactersA
==
0
, extracts no charactersA
>
0
, extracts min(A,n)
The goal is to empty the current buffer, and to not request any more from the external input sequence controlled by the streambuf.
__istream_type& std::basic_istream::seekg | ( | pos_type | ) | [inherited] |
Changing the current read position.
pos | A file position object. |
If fail()
is not true, calls rdbuf()->pubseekpos(pos)
. If that function fails, sets failbit.
gcount()
. __istream_type& std::basic_istream::seekg | ( | off_type | , |
ios_base::seekdir | |||
) | [inherited] |
Changing the current read position.
off | A file offset object. |
dir | The direction in which to seek. |
If fail()
is not true, calls rdbuf()->pubseekoff(off,dir)
. If that function fails, sets failbit.
gcount()
. int std::basic_istream::sync | ( | void | ) | [inherited] |
Synchronizing the stream buffer.
If rdbuf()
is a null pointer, returns -1.
Otherwise, calls rdbuf()->pubsync()
, and if that returns -1, sets badbit and returns -1.
Otherwise, returns 0.
gcount()
. pos_type std::basic_istream::tellg | ( | void | ) | [inherited] |
Getting the current read position.
If fail()
is not false, returns pos_type
(-1) to indicate failure. Otherwise returns rdbuf()->pubseekoff(0,cur,in)
.
gcount()
. At variance with putback, unget and seekg, eofbit is not cleared first. __istream_type& std::basic_istream::unget | ( | void | ) | [inherited] |
Unextracting the previous character.
If rdbuf()
is not null, calls rdbuf()->sungetc(c)
.
If rdbuf()
is null or if sungetc()
fails, sets badbit in the error state.
gcount()
will return 0, as required by DR 60. streamsize std::basic_istream::_M_gcount [protected, inherited] |