C++/istream

维基教科书,自由的教学读本
< C++

istreamw:C++標準程式庫中的一個w:头文件,定义了C++标准中的文件流输入的几个模板类与类型。

类模板[编辑]

下述声明中,g为get的缩写,p为put的缩写。

  • basic_istream: 输入流
    • 运算符函数
      • operator>> 从流中读取指定类型的数据。注意,空白符被忽略。
    • 成员函数:
      • gcount 返回上一次无格式化读取中的字符数量
      • get 4个重载版本。读取1个字符,包含空白符;或者读取多个字符到缓冲区,直至回车符、EOF或指定截止字符(均不含)。截止符保留在流中。
      • getline 读取一行字符或至指定截止字符(均不含)。截止符舍弃。
      • ignore 从当前位置忽略掉指定个数的字符
      • peek 查看下一个要读出的字符
      • putback 把一个字符放回到输入流中。会在读取操作发生之前检查,参数 istream &​ c是否是最后一个读入的字符。
      • read 无格式地严格读取n个字符,遇EOF抛出异常
      • readsome 不阻塞地读取最多n个字符,或至EOF(不抛出异常)
      • seekg 在流中移动读指针的位置,可以绝对寻址或相对寻址。
      • sentry 成员类,做一些异常安全的前置和后置工作。 所有输入操作的成员函数在执行前都会构造一个sentry对象.[1]如果该对象构造成功,会对该对象求bool值。仅当bool值为真,输入操作函数会继续执行;否则函数返回不执行输入操作。如果该对象构造失败,格式化函数会根据情况设置badbit位,并抛出异常。成员函数返回之前,会摧毁这个sentry对象。
      • swap 交换basic_istream对象
      • sync 将缓冲区的内容与关联的字符流同步。内部调用顺序是istream::sync -> basic_streambuf::pubsync -> basic_streambuf::sync。如果是标准输入流,则是将缓冲区的内容清空,保持与键盘输入的同步; 如果是文件流,则是将缓冲区内容与文件内容同步。
      • tellg 获得流输入位置,基于0.
      • unget 把最近读入的字符放回流中
  • basic_iostream: 输入/输出流。多继承了basic_istream和basic_ostream,并定义了几个protected的移动赋值运算符和swap成员函数。

[编辑]

  • istream: 输入流(类)
  • iostream: 输入/输出流(类)
  • wistream: 输入流(宽字符) (类)
  • wiostream: 输入/输出流(宽字符) (类)

函数模板[编辑]

  • ws: 抽取空白符

类型定义[编辑]

  • typedef basic_istream<char> istream
  • typedef basic_istream<wchar_t> wistream
  • typedef basic_iostream<char> iostream
  • typedef basic_iostream<wchar_t> wiostream

参考文献[编辑]

  1. sentry对象的构造函数会执行: If any of its internal error flags is set, the function sets its failbit flag and returns. If it is a tied stream, the function flushes the stream it is tied to (if its output buffer is not empty). The class may implement ways for library functions to defer this flush until the next call to overflow by its associated stream buffer. If its skipws format flag is set, and the constructor is not passed true as second argument (noskipws), all leading whitespace characters (locale-specific) are extracted and discarded. If this operation exhausts the source of characters, the function sets both the failbit and eofbit internal state flags