IE盒子

搜索
查看: 99|回复: 0

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

[复制链接]

1

主题

7

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2022-12-2 14:06:24 | 显示全部楼层 |阅读模式
根据前文,istream类是c++标准输入流的一个基类,本篇详细介绍istream类的主要成员函数用法。
1.istream的构造函数

从istream头文件中截取一部分关于构造函数的声明和定义,如下:
1public:
2explicit
3      basic_istream(__streambuf_type* __sb)
4      : _M_gcount(streamsize(0))
5      { this->init(__sb); }
6protected:
7      basic_istream()
8      : _M_gcount(streamsize(0))
9      { this->init(0); }
10
11#if __cplusplus >= 201103L
12      basic_istream(const basic_istream&) = delete;
13
14      basic_istream(basic_istream&& __rhs)
15      : __ios_type(), _M_gcount(__rhs._M_gcount)
16      {
17    __ios_type::move(__rhs);
18    __rhs._M_gcount = 0;
19      }
20
21      // 27.7.3.3 Assign/swap
22
23      basic_istream& operator=(const basic_istream&) = delete;
24
25      basic_istream&
26      operator=(basic_istream&& __rhs)
27      {
28    swap(__rhs);
29    return *this;
30      }
可以看到istream类的默认构造函数是保护类型,而带参数的构造函数则是公有的,根据public和protected的功能,我们要定义一个istream对象,必须要在参数中传入streambuf类型的指针才可以,否则会报编译错误。
一个可用的例子如下:
1#include <iostream>
2#include <fstream>
3
4using namespace std;
5
6int main()
7{
8    filebuf buf;
9    istream iii(&buf);
10    return 0;
11}
这里应该有人会疑惑,怎么构造函数传的是filebuf类型的入参呢,原因是streambuf的构造函数也是保护类型,且只有一个无参构造函数,所以streambuf是不能直接定义一个对象的,需要使用它的继承者stringbuf或者filebuf,这里使用了filebuf。
另外需要注意的是istream类的拷贝构造函数和赋值函数也都是保护类型的,所以istream是不允许拷贝或者赋值的,所以它也不能直接作为返回类型和参数传递,很多时候需要使用引用来进行传递。
2.右移位>>操作符

部分重载>>操作符函数原型如下:
1    //重载一系列>>操作符,读取各种数据类型的数据放入输入流中
2      __istream_type&
3      operator>>(__istream_type& (*__pf)(__istream_type&))
4      { return __pf(*this); }
5
6      __istream_type&
7      operator>>(__ios_type& (*__pf)(__ios_type&))
8      {
9    __pf(*this);
10    return *this;
11      }
12
13      __istream_type&
14      operator>>(ios_base& (*__pf)(ios_base&))
15      {
16    __pf(*this);
17    return *this;
18      }
19
20      __istream_type&
21      operator>>(bool& __n)
22      { return _M_extract(__n); }
23
24      __istream_type&
25      operator>>(short& __n);
26
27      __istream_type&
28      operator>>(unsigned short& __n)
29      { return _M_extract(__n); }
30
31      __istream_type&
32      operator>>(int& __n);
33
34      __istream_type&
35      operator>>(unsigned int& __n)
36      { return _M_extract(__n); }
37
38      __istream_type&
39      operator>>(long& __n)
40      { return _M_extract(__n); }
41
42      __istream_type&
43      operator>>(unsigned long& __n)
44      { return _M_extract(__n); }
45
46#ifdef _GLIBCXX_USE_LONG_LONG
47      __istream_type&
48      operator>>(long long& __n)
49      { return _M_extract(__n); }
50
51      __istream_type&
52      operator>>(unsigned long long& __n)
53      { return _M_extract(__n); }
54#endif
55      __istream_type&
56      operator>>(float& __f)
57      { return _M_extract(__f); }
58
59      __istream_type&
60      operator>>(double& __f)
61      { return _M_extract(__f); }
62
63      __istream_type&
64      operator>>(long double& __f)
65      { return _M_extract(__f); }
>>操作符可用于从缓冲区提取数据并存储在变量中,使用例子如下:
1#include <iostream>
2#include <fstream>
3
4using namespace std;
5
6int main()
7{
8    filebuf buf;
9    //aaa.txt是文本文件,内容是1234
10    if ( buf.open("aaa.txt", ios::in) == nullptr )
11    {
12        cout << "打开文件出错" << endl;
13        return -1;
14    }
15    istream is(&buf);
16    char c = 0x00;
17    int i = 0;
18    is >> c >> i;
19    cout << "c=" << c << endl << "i=" << i << endl;
20    return 0;
21}
输出结果如下:
1c=1
2i=234
到这里,其实看到is这个变量的用法很熟悉,就是我们常用的cin的用法,因为cin它就是istream类型的对象嘛,这里我们可以大概猜测一下cin是怎么实现的,比如:
1#include <iostream>
2#include <fstream>
3
4using namespace std;
5
6int main()
7{
8    filebuf buf;
9    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
10    {
11        cout << "打开文件出错" << endl;
12        return -1;
13    }
14    istream is(&buf);
15    char c = 0x00;
16    int i = 0;
17    is >> c >> i;
18    cout << "c=" << c << endl << "i=" << i << endl;
19    return 0;
20}
执行程序以后,会等待输入,然后在输出,如下:
1123
2c=1
3i=23
从键盘输入123,回车以后,输出了我们想要的结果,这样就实现了跟cin一样的功能。
/proc/self/fd/0是linux系统中标准输入文件,所以打开这个文件操作的话,反映在命令中中,就是在等待输入。
3.get函数

istream头文件中截取get函数声明,如下:
1      //从输入流中读取一个字符(包括空白字符)并返回,若遇到结束符则返回eof()
2      int_type
3      get();
4
5      //从输入流中读取一个字符并存储在引用参数__C中,如果遇到文件结束符,则__C为eof(),返回this指针
6      __istream_type&
7      get(char_type& __c);
8      //从输入流中读取字符存储在__s指向的内存中,直到输入流被读取完或者读到了__n-1个字符才返回,其中如果在读取字符的过程中遇到了__delim所代表的字符,则提前返回,也就是说__delim相当于是一个终止字符
9      __istream_type&
10      get(char_type* __s, streamsize __n, char_type __delim);
11
12      //从输入流中读取字符存储在__s指向的内存中,直到输入流被读取完或者读到了__n-1个字符才返回,其中如果遇到换行符,则提前返回,从实现看,可见就是上面那个函数的终止字符是换行符
13      __istream_type&
14      get(char_type* __s, streamsize __n)
15      { return this->get(__s, __n, this->widen('\n')); }
16
17      //从输入流中读取字符存储在streambuf对象__sb中,与终止字符__delim返回
18      __istream_type&
19      get(__streambuf_type& __sb, char_type __delim);
20
21      //同理,是以上函数终止字符为换行符
22      __istream_type&
23      get(__streambuf_type& __sb)
24      { return this->get(__sb, this->widen('\n')); }
get函数部分使用例子如下:
1#include <iostream>
2#include <fstream>
3
4using namespace std;
5
6int main()
7{
8    filebuf buf;
9    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
10    {
11        cout << "打开文件出错" << endl;
12        return -1;
13    }
14    istream is(&buf);
15    char g1 = 0x00;
16    g1 = is.get();
17    cout << "g1=" << g1 << endl;
18    is.ignore();//该函数是用于忽略换行符,避免下一次读取会读到换行符
19    char g2 = 0x00;
20    is.get(g2);
21    cout << "g2=" << g2 <<endl;
22    is.ignore();
23    char g3[12] = {0};
24    is.get(g3, sizeof(g3), 'n');
25    cout << "g3=" << g3 <<endl;
26    return 0;
27}
4.getline函数用法

getline函数原型如下:
1      //读取一行的字符串放入__s指向的内存中,遇到终止字符__delim提前结束
2      __istream_type&
3      getline(char_type* __s, streamsize __n, char_type __delim);
4
5      //读取一行的字符串放入__s指向的内存中,遇到换行符提前结束,相当于直接读取一行了
6      __istream_type&
7      getline(char_type* __s, streamsize __n)
8      { return this->getline(__s, __n, this->widen('\n')); }
用法如下:
1#include <iostream>
2#include <fstream>
3
4using namespace std;
5
6int main()
7{
8    filebuf buf;
9    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
10    {
11        cout << "打开文件出错" << endl;
12        return -1;
13    }
14    istream is(&buf);
15    char g3[12] = {0};
16    is.getline(g3, sizeof(g3));
17    cout << "g3=" << g3 <<endl;
18    return 0;
19}
这里就是使用了重载的第二个getline函数,默认遇换行符结束。
此时我们输入:1234567,结果如下:
11234567
2g3=1234567
5.ignore函数和peek函数

函数原型如下:
1//忽略输入流中的__n个字符,遇到字符__delim停止忽略并返回
2      __istream_type&
3      ignore(streamsize __n, int_type __delim);
4      //忽略输入流中的__n个字符
5      __istream_type&
6      ignore(streamsize __n);
7
8      //忽略输入流中字符
9      __istream_type&
10      ignore();
11      //查看输入流中的下一个字符,但不会从输入流中取出来,字符指针位置也不会发生变化,就是看一眼
12      int_type
13      peek();
使用方法如下:
1#include <iostream>
2#include <fstream>
3
4using namespace std;
5
6int main()
7{
8    filebuf buf;
9    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
10    {
11        cout << "打开文件出错" << endl;
12        return -1;
13    }
14    istream is(&buf);
15    char g1 = 0x00;
16    char g3[12] = {0};
17    is >> g1;
18    cout << "g1=" << g1 << endl;
19    is.ignore(2);
20    is.ignore();
21    char g2 = 0x00;
22    g2 = is.peek();
23    cout << "g2=" << g2 << endl;
24    is.get(g3, sizeof(g3));
25    cout << "g3=" << g3 << endl;
26    return 0;
27}
操作结果如下:
11234567
2g1=1
3g2=5
4g3=567
从这里我们可以知道ignore()不带参数的是忽略一个字符,带参数就是忽略n个字符,而peek只是取出了字符,但并没有移动字符指针。
6.read函数和readsome函数

1//读取__n长度的字符串保存在__s中,直到读取完成__n个字符或者遇到文件结束符,eofbit及failbit都被置为1
2      __istream_type&
3      read(char_type* __s, streamsize __n);
4      /*提取字符存储在__s中,能提取多少取决于streambuf缓冲区中剩余的字符数,查看剩余字符数可使用rdbuf()->in_avail(),rdbuf()就是缓冲区,in_avail()返回还有多少没有处理的字符,rdbuf()->in_avail()取值有如下几种情况:
5      -1  说明遇到文件结束符或者没有可提取字符
6      0   说明无可提取字符
7      >0  可提取min(rdbuf()->in_avail(), __n)个字符
8      readsome函数返回实际提取的字符数
9      */
10      streamsize
11      readsome(char_type* __s, streamsize __n);
使用例子如下:
1#include <iostream>
2#include <fstream>
3
4using namespace std;
5
6int main()
7{
8    filebuf buf;
9    if ( buf.open("/proc/self/fd/0", ios::in) == nullptr )
10    {
11        cout << "打开文件出错" << endl;
12        return -1;
13    }
14    istream is(&buf);
15    char szRead[12] = {0};
16    is.read(szRead, sizeof(szRead));
17    cout << "szRead=" << szRead << endl;
18    return 0;
19}
如果键盘输入不够12个字符,read函数读取不会返回,知道读取12个字符为止。
而如果read函数换成readsome函数,就会直接返回,并不会等待输入,也就是目前缓冲区有多少那么读多少,没有也不等待。
7.putback函数、unget函数、sync函数

函数原型如下:
1    //将前面从输入流中读取的字符__C返回到输入流,插入到当前指针位置,注意返回的字符一定要是之前读取过的,否则是不起作用的
2      __istream_type&
3      putback(char_type __c);
4
5      //恢复上一个被读取的字符,重新放回到输入流中,恢复到它原本所在的位置
6      __istream_type&
7      unget();
8
9      //刷新streambuf缓冲区,丢弃剩余没有读取的字符
10      int
11      sync();
12
13      //返回上一次从流中提取的字节数
14      streamsize
15      gcount() const
16      { return _M_gcount; }
实例如下:
1#include <iostream>
2#include <fstream>
3
4using namespace std;
5
6int main()
7{
8    filebuf buf;
9    if ( buf.open("aaa.txt", ios::in) == nullptr )
10    {
11        cout << "打开文件出错" << endl;
12        return -1;
13    }
14    istream is(&buf);
15    char aa = 0x00;
16    char szRead[12] = {0};
17    int i = 0;
18    is >> aa;
19    cout << "aa=" << aa << endl;
20    is >> aa;
21    cout << "aa=" << aa << endl;
22    is.putback('1');
23    is >> aa;
24    cout << "aa=" << aa << endl;
25    is.unget();
26    is >> i;
27    cout << "i=" << i << endl;
28    return 0;
29}
文件aaa.txt内容是1234,输出结果如下:
1aa=1
2aa=2
3aa=1
4i=134
从结果可以看出putback可以放回之前提取的任意一个字符,而unget是直接放回上一个提取的字符。
8.tellg和seekg函数

1      //返回当前字符指针的位置
2      pos_type
3      tellg();
4
5      //根据参数跳转当前字符指针位置,默认按照开始位置跳转
6      __istream_type&
7      seekg(pos_type);
8
9      //根据参数跳转当前字符指针位置,ios_base::seekdir标示从什么位置开始跳转
10      __istream_type&
11      seekg(off_type, ios_base::seekdir);
一个实际使用案例如下:
1#include <iostream>
2#include <fstream>
3
4using namespace std;
5
6int main()
7{
8    filebuf buf;
9    if ( buf.open("aaa.txt", ios::in) == nullptr )
10    {
11        cout << "打开文件出错" << endl;
12        return -1;
13    }
14    istream is(&buf);
15    char aa = 0x00;
16    char bb = 0x00;
17    aa = is.get();
18    bb = is.get();
19    cout << "tellg=" << is.tellg() << endl;
20    is.seekg(1, ios::beg);//从文件开始处跳转一个位置
21    cout << "tellg=" << is.tellg() << endl;
22
23    return 0;
24}
结果如下:
1tellg=2
2tellg=1
到这里,istream类的public成员函数就介绍完毕啦,若有不对之处,欢迎指正。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表