what does rdstate() return value means?

后端 未结 2 633
忘掉有多难
忘掉有多难 2021-02-09 19:12
istream& Read(istream &is)
{
    std::string buf;
    while (is >> buf)       
    {   
        cout << is.eofbit << \" \" << is.failbit          


        
2条回答
  •  故里飘歌
    2021-02-09 20:07

    The member std::ios::rdstate() simply returns a combination of the state flags std::ios_base::badbit, std::ios_base::eofbit, and std::ios_base::failbit.But the size of each state flag is not 1 bit, the type of all three state flags are enum std::_Iosb::_Iostate, which is machine-dependent integral as well as std::strm::iostate. On my machine, they account for 4 bytes.

    But I don't understand what its output means

    when only badbit gets set explicity or directly, the value of s.rdstate()is 4. when only failbit gets set explicity or directly, the value of s.rdstate() is 2. when only eofbit gets set explicity or directly, the value of s.rdstate() is 1.

    when several parts of them get set explicity or directly, the value of s.rdstate() can calculated from the sum of the corresponding value, namely, the 1st bit of s.rdstate()indicates the state ofeofbit,the 2nd bit of s.rdstate()indicates the state offailbit,the 3th bit of s.rdstate()indicates the state ofbadbit.

    But the interesting thing is when the badbit gets set explicitly or directly, the failbit will get set in response(not explicity or directly).At this time, the value of s.rdstate() do not count failbit in. Read my code and see the output, you will understand what I mean.

    #include
    #include
    
    std::istream & print(std::istream &is) {
        static unsigned cnt = 0;
        ++cnt;
        std::cout << cnt << ((cnt % 10 == 1) ? "st" :
            (cnt % 10 == 2) ? "nd" : "th")
            << " call print" << "\n";
    
        std::cout<< "is.badbit: " << is.badbit << "\n"
            << "is.failbit: " << is.failbit << "\n"
            << "is.eofbit: " << is.eofbit << "\n"
            << "is.bad(): " << is.bad() << "\n" 
            << "is.fail(): " << is.fail() << "\n"
            << "is.eof(): " << is.eof() << "\n"
            << "is.rdstate(): " << is.rdstate() << "\n";
        return is;
    }
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::vector;
    
    int main()
    {
        cout << "sizeof(iostate): " <

    The output is:

    sizeof(iostate): 4
    sizeof(goodbit): 4
    
    1st call print
    is.badbit: 4
    is.failbit: 2
    is.eofbit: 1
    is.bad(): 0
    is.fail(): 0
    is.eof(): 0
    is.rdstate(): 0
    
    2nd call print
    is.badbit: 4
    is.failbit: 2
    is.eofbit: 1
    is.bad(): 1
    is.fail(): 1
    is.eof(): 0
    is.rdstate(): 4
    
    3th call print
    is.badbit: 4
    is.failbit: 2
    is.eofbit: 1
    is.bad(): 0
    is.fail(): 1
    is.eof(): 0
    is.rdstate(): 2
    
    4th call print
    is.badbit: 4
    is.failbit: 2
    is.eofbit: 1
    is.bad(): 0
    is.fail(): 0
    is.eof(): 1
    is.rdstate(): 1
    
    5th call print
    is.badbit: 4
    is.failbit: 2
    is.eofbit: 1
    is.bad(): 1
    is.fail(): 1
    is.eof(): 0
    is.rdstate(): 6
    
    6th call print
    is.badbit: 4
    is.failbit: 2
    is.eofbit: 1
    is.bad(): 1
    is.fail(): 1
    is.eof(): 1
    is.rdstate(): 5
    
    7th call print
    is.badbit: 4
    is.failbit: 2
    is.eofbit: 1
    is.bad(): 0
    is.fail(): 1
    is.eof(): 1
    is.rdstate(): 3
    

提交回复
热议问题