The C++ std::ios::init() function is used to initialize the input/output stream object internal state. It is called ny stream constructors and assignment operators to set up the streams buffer, errors. It ensures that the stream is in a usable state before any operations are performed on it.
The init() function is not required for standard use cases, as the streams are initialized automatically by the c++ library.
The internal state is initialized in such a way that each of these members return the following values −
member function return value rdbuf sb tie0
rdstate goodbit if sb is not a null pointer, badbit otherwise exceptions goodbit flags skipws | dec
width 0
precision 6
fill ' '
(whitespace) getloc a copy of locale()
Syntax
Following is the syntax for std::ios::init() function.
protected:void init (streambuf* sb);Parameters
This function does not return anything.
ExceptionsIf an exception is thrown, the stream is in a valid state.
Data racesModifies the stream object. The object pointed by sb may be accessed and/or modified.
Concurrent access to the same stream object or stream buffer may cause data races.
ExampleIn the following example, we are going to consider the basic usage of the init() function.
#include <iostream> int main() { std::ostream a(nullptr); a.setstate(std::ios::failbit); std::cout << "TutorialsPoint." << std::endl; return 0; }Output
Output of the above code is as follows −
TutorialsPoint.
ios.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4