ios setstate() function in C++ with Examples
Last Updated :
21 Mar, 2023
The setstate() method of ios class in C++ is used to change the current state of this stream by setting the flags passed as the parameters. Hence this function changes the internal state of this stream. Syntax:
void setstate(iostate state)
Parameters: This method accepts the iostate as parameter which is the combination of goodbit, failbit, eofbit and badbit flags to be set in this stream. Return Value: This method do not return anything.
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 1:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
stringstream ss;
ss.clear(ss.goodbit);
stringstream ss2;
cout << "current stream: " << ss2.rdstate() << endl;
ss2.setstate(ss.rdstate());
cout << "updated stream: " << ss2.rdstate() << endl;
return 0;
}
|
Output:
current stream: 0
updated stream: 0
Example 2:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
stringstream ss;
ss.clear(ss.failbit);
stringstream ss2;
cout << "current stream: " << ss2.rdstate() << endl;
ss2.setstate(ss.rdstate());
cout << "updated stream: " << ss2.rdstate() << endl;
return 0;
}
|
Output:
current stream: 0
updated stream: 4
Reference: hhttp://www.cplusplus.com/reference/ios/ios/setstate/
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...