c++11 标准模板(STL)(std::ios_base)成员类型与常量

流打开模式类型 

std::ios_base::openmode

typedef /*implementation defined*/ openmode;

static constexpr openmode app = /*implementation defined*/

static constexpr openmode binary = /*implementation defined*/
static constexpr openmode in = /*implementation defined*/
static constexpr openmode out = /*implementation defined*/
static constexpr openmode trunc = /*implementation defined*/

static constexpr openmode ate = /*implementation defined*/

指定可用的文件打开标志。它是位掩码类型 (BitmaskType) ,下列常量得到定义:

常量 解释
app 每次写入前寻位到流结尾
binary 以二进制模式打开
in 为读打开
out 为写打开
trunc 在打开时舍弃流的内容
ate 打开后立即寻位到流结尾

格式化标志类型

std::ios_base::fmtflags

typedef /*implementation defined*/ fmtflags;

static constexpr fmtflags dec = /*implementation defined*/

static constexpr fmtflags oct = /*implementation defined*/
static constexpr fmtflags hex = /*implementation defined*/

static constexpr fmtflags basefield = dec | oct | hex;
static constexpr fmtflags left = /*implementation defined*/

static constexpr fmtflags right = /*implementation defined*/
static constexpr fmtflags internal = /*implementation defined*/

static constexpr fmtflags adjustfield = left | right | internal;
static constexpr fmtflags scientific = /*implementation defined*/

static constexpr fmtflags fixed = /*implementation defined*/

static constexpr fmtflags floatfield = scientific | fixed;
static constexpr fmtflags boolalpha = /*implementation defined*/

static constexpr fmtflags showbase = /*implementation defined*/
static constexpr fmtflags showpoint = /*implementation defined*/
static constexpr fmtflags showpos = /*implementation defined*/
static constexpr fmtflags skipws = /*implementation defined*/
static constexpr fmtflags unitbuf = /*implementation defined*/

static constexpr fmtflags uppercase = /*implementation defined*/

指定可用的格式化标志。它是位掩码类型 (BitmaskType) 。定义下列常量:

常量 解释
dec 为整数 I/O 使用十进制底:见 std::dec
oct 为整数 I/O 使用八进制底:见 std::oct
hex 为整数 I/O 使用十六进制底:见 std::hex
basefield dec|oct|hex 。适用于掩码运算
left 左校正(添加填充字符到右):见 std::left
right 右校正(添加填充字符到左):见 std::right
internal 内部校正(添加填充字符到内部选定点):见 std::internal
adjustfield left|right|internal 。适用于掩码运算
scientific 用科学记数法生成浮点类型,或若与 fixed 组合则用十六进制记法:见 std::scientific
fixed 用定点记法生成浮点类型,或若与 scientific 组合则用十六进制记法:见 std::fixed
floatfield scientific|fixed 。适用于掩码运算
boolalpha 以字母数字格式插入并释出 bool 类型:见 std::boolalpha
showbase 生成为整数输出指示数字基底的前缀,货币 I/O 中要求现金指示器:见 std::showbase
showpoint 无条件为浮点数输出生成小数点字符:见 std::showpoint
showpos 为非负数值输出生成 + 字符,见 std::showpos
skipws 在具体输入操作前跳过前导空白符:见 std::skipws
unitbuf 在每次输出操作后冲入输出:见 std::unitbuf
uppercase 在具体输出的输出操作中以大写等价替换小写字符:见 std::uppercase

流状态类型

std::ios_base::iostate

typedef /*implementation defined*/ iostate;

static constexpr iostate goodbit = 0;

static constexpr iostate badbit = /*implementation defined*/

static constexpr iostate failbit = /*implementation defined*/

static constexpr iostate eofbit = /*implementation defined*/

指定流状态标志。它是位掩码类型 (BitmaskType) ,定义下列常量:

常量 解释
goodbit 无错误
badbit 不可恢复的流错误
failbit 输入/输出操作失败(格式化或提取错误)
eofbit 关联的输出序列已抵达文件尾

eofbit

下列标准库函数设置 eofbit :

  • string 输入函数 std::getline ,若它以抵达流结尾,而非抵达指定的终止字符完成。
  • basic_istream::operator>> 的数值输入重载,若在 num_get::get 处理的阶段 2 ,读取下个字符时遇到流结尾。取决于分析状态,可能或可能不同时设置 failbit :例如 int n; istringstream buf("1"); buf >> n; 设置 eofbit ,但不设置 failbit :成功分析整数 1 并存储之于 n 。另一方面, bool b; istringstream buf("tr"); buf >> boolalpha >> b; 一同设置 eofbitfailbit :无足够的字符完成布尔 true 的分析。
  • operator>>std::basic_istream 的字符释出重载,若在释出字符数量上的限制(若存在)前抵达流结尾。
  • std::get_time I/O 操纵符和任何 std::time_get 分析函数: time_get::get 、 time_get::get_time 、 time_get::get_date 等,若在分析期待的被处理日期/时间值所需的最末字符前抵达流结尾。
  • std::get_money I/O 操纵符和 money_get::get 函数,若在分析期待的被处理货币值所需的最末字符前抵达流结尾。
  • basic_istream::sentry 构造函数,在每个有格式输入函数的起始执行:除非未设置 skipws (例如通过发布 std::noskipws ),否则 sentry 会读取并舍弃前导空白字符。若在此操作中抵达流结尾,则一同设置 eofbitfailbit ,并且不发生输入。
  • I/O 操纵符 std::ws ,若它在消耗空白符时抵达流结尾(但不同于有格式输入 sentry ,此情况下它不设置 failbit
  • 无格式输入函数 basic_istream::read 、 basic_istream::get 、 basic_istream::peek 和 basic_istream::getline 在抵达流尾时。
  • 舍弃输入函数 basic_istream::ignore ,在抵达指定的分隔字符前抵达流结尾时。
  • 立即输入函数 basic_istream::readsome ,若 basic_streambuf::in_avail 返回 -1

下列函数作为副效应清除 eofbit

  • basic_istream::putback
  • basic_istream::unget
  • basic_istream::seekg

注意在几乎所有情况下,若设置 eofbit ,则一同设置 failbit 。

failbit

下列标准库函数设置 failbit :

  • basic_istream::sentry 构造函数,在每个输入函数起始执行,若流上已设置 eofbitbadbit ,或若在消耗前导空白时遇到流结尾。
  • basic_ostream::sentry 构造函数,在每个输出函数起始执行,在实现定义的条件下。
  • operator>>(std::basic_string<>) ,若函数未从输入流释出字符。
  • operator>>(std::complex<>) ,若函数无法释出合法的复数。
  • operator>> 的字符数组和单字符重载,若它们无法释出字符。
  • basic_istream::operator>> 的 streambuf 重载,若 streambuf 参数为空指针或若没有插入字符到 streambuf 。
  • basic_ostream::operator<< 的 streambuf 重载,若函数未插入字符。
  • operator>>(std::bitset<>) ,若函数未从输出流释出字符。
  • std::getline ,若函数未释出字符,或若它要从输入流释出 basic_string::max_size 个字符。
  • basic_istream::operator>> 的数值、指针和布尔输入重载(技术上是它们调用的 num_get::get 的重载),若输入不能分析为合法值,或若分析出的值不适合于目标类型。
  • 时间输入操纵符 std::get_time (技术上是其所调用的 time_get::get ),若输入不能按照给定的格式字符串,无歧义地分析为时间值。
  • 货币输入操纵符 std::get_money (技术上是其所调用的 money_get::get ),若输入不能按照本地环境规则无歧义地分析为货币值。
  • 所有随机数引擎 (RandomNumberEngine) 的释出函数,若遇到错误输入。
  • 所有随机数分布 (RandomNumberDistribution) 的释出函数,若遇到错误输入。
  • 无格式输入函数 basic_istream::get ,若它们无法释出任何字符。
  • basic_istream::getline ,若它未释出字符,若它填充给定的缓冲区而未遇到分隔符,或若提供的缓冲区大小小于 1 。
  • basic_istream::read ,若在能释出所有请求的字符前,输入流上出现文件尾条件。
  • basic_istream::seekg 在失败时。
  • basic_ostream::tellp 在失败时。
  • std::basic_fstream 、 std::basic_ifstream 和 std::basic_ofstream 接收文件名参数的构造函数,若无法打开文件。
  • basic_fstream::open 、 basic_ifstream::open 和 basic_ofstream::open ,若无法打开文件。
  • basic_fstream::close 、 basic_ifstream::close 和 basic_ofstream::close ,若无法关闭文件。

badbit

下列标准库函数设置 badbit :

  • basic_ostream::put ,若它因任何原因无法插入元素到输出流。
  • basic_ostream::write ,若它因任何原因无法插入元素到输出流。
  • 有格式输出函数 operator<< 、 std::put_money 和 std::put_time ,若它们在完成输出前遇到输出流的结尾。
  • basic_ios::init ,在以对于 rdbuf() 的空指针调用以初始化流时。
  • basic_istream::putback 和 basic_istream::unget ,在以空的 rdbuf() 在流上调用时
  • basic_ostream::operator<<(basic_streambuf*) ,传递空指针为参数时。
  • basic_istream::putback 和 basic_istream::unget ,若

rdbuf()->sputbackc() 或 rdbuf()->sungetc() 返回 traits::eof() 。

  • basic_istream::sync 、 basic_ostream::flush 和 unitbuf 输出流上的每个输出函数,若 rdbuf()->pubsync() 返回 -1 。
  • 每个流 I/O 函数,若任何关联流缓冲的成员函数(如 sbumpc() 、 xsputn() 、 sgetc() 、 overflow() 等)抛出异常。
  • ios_base::iword 和 ios_base::pword 在失败时(例如无法分配内存)。

寻位方向类型

std::ios_base::seekdir

typedef /*implementation defined*/ seekdir;

static constexpr seekdir beg = /*implementation defined*/

static constexpr seekdir end = /*implementation defined*/

static constexpr seekdir cur = /*implementation defined*/

 指定文件寻位方向类型。定义下列常量:

常量 解释
beg 流的开始
end 流的结尾
cur 流位置指示器的当前位置

指定事件类型

std::ios_base::event

enum event {erase_event, imbue_event, copyfmt_event};

指定在特殊事件时传递给 register_callback() 所注册函数的事件类型。定义下列常量:

常量 解释
erase_event 在 ~ios_base() 或 basic_ios::copyfmt() (发生成员复制前)时发行
imbue_event 在 imbue() 时发行
copyfmt_event 在 basic_ios::copyfmt() (发生成员复制后,但在复制异常设置前)发行

回调函数类型

std::ios_base::event_callback

typedef void (*event_callback)(event type, ios_base& ios, int index);

能用 register_callback() 注册以在特殊事件时调用的函数回调类型。

type 是 ios_base::event 类型值,指示将调用此回调的事件类型。

ios 指代调用回调所用的流对象: std::ios_base 和 std::basic_ios 的成员函数调用回调时,传递 *this 为参数。

index 是注册函数时传递给 register_callback() 的用户提供值。

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/131146349
今日推荐