STL之--输入输出流状态的相关函数操作

本文介绍了C++中用于控制输入输出流的几个关键函数,包括clear()函数,用于更改cin的状态标识符;ignore()函数,用于读取并抛弃残留在流缓冲区中的字符;以及system()系列函数,用于实现等待用户按键和清屏等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • clear()函数

    功能:更改cin的状态标识符,使之处于正确状态。
    定义在<ios>头文件中,是basic_ios类的成员函数,以下是相关的源码:

// TEMPLATE CLASS basic_ios
template<class _Elem,class _Traits>
    class basic_ios: public ios_base
{   // base class for basic_istream/basic_ostream
public:
    typedef basic_ios<_Elem, _Traits> _Myt;
    typedef basic_ostream<_Elem, _Traits> _Myos;
    typedef basic_streambuf<_Elem, _Traits> _Mysb;
    typedef ctype<_Elem> _Ctype;
    typedef _Elem char_type;
    typedef _Traits traits_type;
    typedef typename _Traits::int_type int_type;
    typedef typename _Traits::pos_type pos_type;
    typedef typename _Traits::off_type off_type;

    explicit __CLR_OR_THIS_CALL basic_ios(_Mysb *_Strbuf)
        {   // construct from stream buffer pointer
        init(_Strbuf);
        }

    virtual __CLR_OR_THIS_CALL ~basic_ios() _NOEXCEPT
        {   // destroy the object
        }

    void __CLR_OR_THIS_CALL clear(iostate _State = goodbit,
        bool _Reraise = false)
        {   // set state, possibly reraise exception
        ios_base::clear((iostate)(_Mystrbuf == 0
            ? (int)_State | (int)badbit : (int)_State), _Reraise);
        }

    void __CLR_OR_THIS_CALL clear(io_state _State)
        {   // set state to _State
        clear((iostate)_State);
        }
//此处省去之后basic_ios的其他成员函数
}
  • ignore()函数
    功能:ignore函数是输入流的成员函数,可用来读取并抛弃残留在流缓冲区中的包括‘\n’在内的字符。
    定义在<istream>头文件中,用法如cin.ignore(numeric_limits<int>::max, ‘\n’),这里用于读取近乎无限个字符,或者遇到了‘\n’,以下是摘取的ignore函数的源码:
_Myt& __CLR_OR_THIS_CALL 
ignore(streamsize _Count = 1,int_type _Metadelim = _Traits::eof())
{   // ignore up to _Count characters, discarding delimiter
        ios_base::iostate _State = ios_base::goodbit;
        _Chcount = 0;
        const sentry _Ok(*this, true);

        if (_Ok && 0 < _Count)
        {   // state okay, use facet to extract 
            _TRY_IO_BEGIN
            for (; ; )
            {   // get a metacharacter if more room in buffer
                int_type _Meta;
                if (_Count != INT_MAX && --_Count < 0)
                    break;  //buffer full, quit
                else if (_Traits::eq_int_type(_Traits::eof(),
                    _Meta = _Myios::rdbuf()->sbumpc()))
                {   // end of file, quit
                    _State |= ios_base::eofbit;
                    break;
                }
                else
                {   // got a character, count it
                    ++_Chcount;
                    if (_Meta == _Metadelim)
                        break;  // got a delimiter, quit
                }
            }
            _CATCH_IO_END
        }
        _Myios::setstate(_State);
        return (*this);
}

其中,_Count指被拋弃字符的最大数目,默认为1;_Metadelim指遇到此字符后ignore函数停止运行(注意:此字符将被拋弃),默认为EOF字符(即遇到文件尾)。

  • system()系列函数
    1.system(“pause>nul”)
    用于在输出时,等按下任意键后才会返回,且不显示”请按任意键继续”;
    2.system(“pause”)
    用于在输出时,等按下任意键后才会返回;
    3.system(“cls”)
    可以实现清屏操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值