c++中istream类的超详细说明

cpp加油站
• 阅读 1093

根据前文,istream类是c++标准输入流的一个基类,本篇详细介绍istream类的主要成员函数用法。

1.istream的构造函数

从istream头文件中截取一部分关于构造函数的声明和定义,如下:

public:
explicit
      basic_istream(__streambuf_type* __sb)
      : _M_gcount(streamsize(0))
      { this->init(__sb); }
protected:
      basic_istream()
      : _M_gcount(streamsize(0))
      { this->init(0); }

#if __cplusplus >= 201103L
      basic_istream(const basic_istream&) = delete;

      basic_istream(basic_istream&& __rhs)
      : __ios_type(), _M_gcount(__rhs._M_gcount)
      {
    __ios_type::move(__rhs);
    __rhs._M_gcount = 0;
      }

      // 27.7.3.3 Assign/swap

      basic_istream& operator=(const basic_istream&) = delete;

      basic_istream&
      operator=(basic_istream&& __rhs)
      {
    swap(__rhs);
    return *this;
      }

可以看到istream类的默认构造函数是保护类型,而带参数的构造函数则是公有的,根据public和protected的功能,我们要定义一个istream对象,必须要在参数中传入streambuf类型的指针才可以,否则会报编译错误。

一个可用的例子如下:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    filebuf buf;
    istream iii(&buf);
    return 0;
}

这里应该有人会疑惑,怎么构造函数传的是filebuf类型的入参呢,原因是streambuf的构造函数也是保护类型,且只有一个无参构造函数,所以streambuf是不能直接定义一个对象的,需要使用它的继承者stringbuf或者filebuf,这里使用了filebuf。

另外需要注意的是istream类的拷贝构造函数和赋值函数也都是保护类型的,所以istream是不允许拷贝或者赋值的,所以它也不能直接作为返回类型和参数传递,很多时候需要使用引用来进行传递。

2.右移位>>操作符

部分重载>>操作符函数原型如下:

    //重载一系列>>操作符,读取各种数据类型的数据放入输入流中
      __istream_type&
      operator>>(__istream_type& (*__pf)(__istream_type&))
      { return __pf(*this); }

      __istream_type&
      operator>>(__ios_type& (*__pf)(__ios_type&))
      {
    __pf(*this);
    return *this;
      }

      __istream_type&
      operator>>(ios_base& (*__pf)(ios_base&))
      {
    __pf(*this);
    return *this;
      }

      __istream_type&
      operator>>(bool& __n)
      { return _M_extract(__n); }

      __istream_type&
      operator>>(short& __n);

      __istream_type&
      operator>>(unsigned short& __n)
      { return _M_extract(__n); }

      __istream_type&
      operator>>(int& __n);

      __istream_type&
      operator>>(unsigned int& __n)
      { return _M_extract(__n); }

      __istream_type&
      operator>>(long& __n)
      { return _M_extract(__n); }

      __istream_type&
      operator>>(unsigned long& __n)
      { return _M_extract(__n); }

#ifdef _GLIBCXX_USE_LONG_LONG
      __istream_type&
      operator>>(long long& __n)
      { return _M_extract(__n); }

      __istream_type&
      operator>>(unsigned long long& __n)
      { return _M_extract(__n); }
#endif
      __istream_type&
      operator>>(float& __f)
      { return _M_extract(__f); }

      __istream_type&
      operator>>(double& __f)
      { return _M_extract(__f); }

      __istream_type&
      operator>>(long double& __f)
      { return _M_extract(__f); }

>>操作符可用于从缓冲区提取数据并存储在变量中,使用例子如下:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    filebuf buf;
    //aaa.txt是文本文件,内容是1234
    if ( buf.open("aaa.txt", ios::in) == nullptr )
    {
        cout << "打开文件出错" << endl;
        return -1;
    }
    istream is(&buf);
    char c = 0x00;
    int i = 0;
    is >> c >> i;
    cout << "c=" << c << endl << "i=" << i << endl;
    return 0;
}

输出结果如下:

c=1
i=234

到这里,其实看到is这个变量的用法很熟悉,就是我们常用的cin的用法,因为cin它就是istream类型的对象嘛,这里我们可以大概猜测一下cin是怎么实现的,比如:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    filebuf buf;
    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
    {
        cout << "打开文件出错" << endl;
        return -1;
    }
    istream is(&buf);
    char c = 0x00;
    int i = 0;
    is >> c >> i;
    cout << "c=" << c << endl << "i=" << i << endl;
    return 0;
}

执行程序以后,会等待输入,然后在输出,如下:

123
c=1
i=23

从键盘输入123,回车以后,输出了我们想要的结果,这样就实现了跟cin一样的功能。

/proc/self/fd/0是linux系统中标准输入文件,所以打开这个文件操作的话,反映在命令中中,就是在等待输入。

3.get函数

istream头文件中截取get函数声明,如下:

      //从输入流中读取一个字符(包括空白字符)并返回,若遇到结束符则返回eof()
      int_type
      get();

      //从输入流中读取一个字符并存储在引用参数__C中,如果遇到文件结束符,则__C为eof(),返回this指针
      __istream_type&
      get(char_type& __c);
      //从输入流中读取字符存储在__s指向的内存中,直到输入流被读取完或者读到了__n-1个字符才返回,其中如果在读取字符的过程中遇到了__delim所代表的字符,则提前返回,也就是说__delim相当于是一个终止字符
      __istream_type&
      get(char_type* __s, streamsize __n, char_type __delim);

      //从输入流中读取字符存储在__s指向的内存中,直到输入流被读取完或者读到了__n-1个字符才返回,其中如果遇到换行符,则提前返回,从实现看,可见就是上面那个函数的终止字符是换行符
      __istream_type&
      get(char_type* __s, streamsize __n)
      { return this->get(__s, __n, this->widen('\n')); }

      //从输入流中读取字符存储在streambuf对象__sb中,与终止字符__delim返回
      __istream_type&
      get(__streambuf_type& __sb, char_type __delim);

      //同理,是以上函数终止字符为换行符
      __istream_type&
      get(__streambuf_type& __sb)
      { return this->get(__sb, this->widen('\n')); }

get函数部分使用例子如下:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    filebuf buf;
    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
    {
        cout << "打开文件出错" << endl;
        return -1;
    }
    istream is(&buf);
    char g1 = 0x00;
    g1 = is.get();
    cout << "g1=" << g1 << endl;
    is.ignore();//该函数是用于忽略换行符,避免下一次读取会读到换行符
    char g2 = 0x00;
    is.get(g2);
    cout << "g2=" << g2 <<endl;
    is.ignore();
    char g3[12] = {0};
    is.get(g3, sizeof(g3), 'n');
    cout << "g3=" << g3 <<endl;
    return 0;
}
4.getline函数用法

getline函数原型如下:

      //读取一行的字符串放入__s指向的内存中,遇到终止字符__delim提前结束
      __istream_type&
      getline(char_type* __s, streamsize __n, char_type __delim);

      //读取一行的字符串放入__s指向的内存中,遇到换行符提前结束,相当于直接读取一行了
      __istream_type&
      getline(char_type* __s, streamsize __n)
      { return this->getline(__s, __n, this->widen('\n')); }

用法如下:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    filebuf buf;
    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
    {
        cout << "打开文件出错" << endl;
        return -1;
    }
    istream is(&buf);
    char g3[12] = {0};
    is.getline(g3, sizeof(g3));
    cout << "g3=" << g3 <<endl;
    return 0;
}

这里就是使用了重载的第二个getline函数,默认遇换行符结束。 此时我们输入:1234567,结果如下:

1234567
g3=1234567
5.ignore函数和peek函数

函数原型如下:

//忽略输入流中的__n个字符,遇到字符__delim停止忽略并返回
      __istream_type&
      ignore(streamsize __n, int_type __delim);
      //忽略输入流中的__n个字符
      __istream_type&
      ignore(streamsize __n);

      //忽略输入流中字符
      __istream_type&
      ignore();
      //查看输入流中的下一个字符,但不会从输入流中取出来,字符指针位置也不会发生变化,就是看一眼
      int_type
      peek();

使用方法如下:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    filebuf buf;
    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
    {
        cout << "打开文件出错" << endl;
        return -1;
    }
    istream is(&buf);
    char g1 = 0x00;
    char g3[12] = {0};
    is >> g1;
    cout << "g1=" << g1 << endl;
    is.ignore(2);
    is.ignore();
    char g2 = 0x00;
    g2 = is.peek();
    cout << "g2=" << g2 << endl;
    is.get(g3, sizeof(g3));
    cout << "g3=" << g3 << endl;
    return 0;
}

操作结果如下:

1234567
g1=1
g2=5
g3=567

从这里我们可以知道ignore()不带参数的是忽略一个字符,带参数就是忽略n个字符,而peek只是取出了字符,但并没有移动字符指针。

6.read函数和readsome函数
//读取__n长度的字符串保存在__s中,直到读取完成__n个字符或者遇到文件结束符,eofbit及failbit都被置为1
      __istream_type&
      read(char_type* __s, streamsize __n);
      /*提取字符存储在__s中,能提取多少取决于streambuf缓冲区中剩余的字符数,查看剩余字符数可使用rdbuf()->in_avail(),rdbuf()就是缓冲区,in_avail()返回还有多少没有处理的字符,rdbuf()->in_avail()取值有如下几种情况:
      -1  说明遇到文件结束符或者没有可提取字符
      0   说明无可提取字符
      >0  可提取min(rdbuf()->in_avail(), __n)个字符
      readsome函数返回实际提取的字符数
      */
      streamsize
      readsome(char_type* __s, streamsize __n);

使用例子如下:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    filebuf buf;
    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
    {
        cout << "打开文件出错" << endl;
        return -1;
    }
    istream is(&buf);
    char szRead[12] = {0};
    is.read(szRead, sizeof(szRead));
    cout << "szRead=" << szRead << endl;
    return 0;
}

如果键盘输入不够12个字符,read函数读取不会返回,知道读取12个字符为止。 而如果read函数换成readsome函数,就会直接返回,并不会等待输入,也就是目前缓冲区有多少那么读多少,没有也不等待。

7.putback函数、unget函数、sync函数

函数原型如下:

    //将前面从输入流中读取的字符__C返回到输入流,插入到当前指针位置,注意返回的字符一定要是之前读取过的,否则是不起作用的
      __istream_type&
      putback(char_type __c);

      //恢复上一个被读取的字符,重新放回到输入流中,恢复到它原本所在的位置
      __istream_type&
      unget();

      //刷新streambuf缓冲区,丢弃剩余没有读取的字符
      int
      sync();

      //返回上一次从流中提取的字节数
      streamsize
      gcount() const
      { return _M_gcount; }

实例如下:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    filebuf buf;
    if ( buf.open("aaa.txt", ios::in) == nullptr )
    {
        cout << "打开文件出错" << endl;
        return -1;
    }
    istream is(&buf);
    char aa = 0x00;
    char szRead[12] = {0};
    int i = 0;
    is >> aa;
    cout << "aa=" << aa << endl;
    is >> aa;
    cout << "aa=" << aa << endl;
    is.putback('1');
    is >> aa;
    cout << "aa=" << aa << endl;
    is.unget();
    is >> i;
    cout << "i=" << i << endl;
    return 0;
}

文件aaa.txt内容是1234,输出结果如下:

aa=1
aa=2
aa=1
i=134

从结果可以看出putback可以放回之前提取的任意一个字符,而unget是直接放回上一个提取的字符。

8.tellg和seekg函数
      //返回当前字符指针的位置
      pos_type
      tellg();

      //根据参数跳转当前字符指针位置,默认按照开始位置跳转
      __istream_type&
      seekg(pos_type);

      //根据参数跳转当前字符指针位置,ios_base::seekdir标示从什么位置开始跳转
      __istream_type&
      seekg(off_type, ios_base::seekdir);

一个实际使用案例如下:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    filebuf buf;
    if ( buf.open("aaa.txt", ios::in) == nullptr )
    {
        cout << "打开文件出错" << endl;
        return -1;
    }
    istream is(&buf);
    char aa = 0x00;
    char bb = 0x00;
    aa = is.get();
    bb = is.get();
    cout << "tellg=" << is.tellg() << endl;
    is.seekg(1, ios::beg);//从文件开始处跳转一个位置
    cout << "tellg=" << is.tellg() << endl;

    return 0;
}

结果如下:

tellg=2
tellg=1

到这里,istream类的public成员函数就介绍完毕啦,若有不对之处,欢迎指正。

点赞
收藏
评论区
推荐文章

暂无数据