C++ IOS::init() function



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 functionreturn value
rdbufsb
tie0
rdstategoodbit if sb is not a null pointer, badbit otherwise
exceptionsgoodbit
flagsskipws | dec
width0
precision6
fill' ' (whitespace)
getloca copy of locale()

Syntax

Following is the syntax for std::ios::init() function.

protected:void init (streambuf* sb);

Parameters

  • sb − It indicates the pointer to a streambuf object.

Return Value

This function does not return anything.

Exceptions

If an exception is thrown, the stream is in a valid state.

Data races

Modifies 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.

Example

In 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