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/
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Mar, 2023
Like Article
Save Article