c++11 标准模板(STL)(std::basic_ios)(五)

定义于头文件 <ios>

template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_ios : public std::ios_base

类 std::basic_ios 提供设施,以对拥有 std::basic_streambuf 接口的对象赋予接口。数个 std::basic_ios 对象能指涉一个实际的 std::basic_streambuf 对象。

继承图

 

还提供了两个对常见的字符类型的特化:

类型定义
iosbasic_ios<char>
wiosbasic_ios<wchar_t>


杂项

管理异常掩码

std::basic_ios<CharT,Traits>::exceptions

std::ios_base::iostate exceptions() const;

(1)

void exceptions( std::ios_base::iostate except );

(2)

 获取和设置流的异常掩码。异常掩码确定在哪些错误状态出现时抛出 failure 类型异常。

1) 返回异常掩码。

2) 设置异常掩码为 except

参数

except-异常掩码

返回值

1) 当前异常掩码。

2) (无)

调用示例

#include <iostream>
#include <fstream>

int main()
{
    int ivalue;
    try
    {
        std::ifstream in("in.txt");
        in.exceptions(std::ifstream::failbit);
        in >> ivalue;
    }
    catch (std::ios_base::failure &fail)
    {
        // 此处处理异常
    }
    return 0;
}

输出

设置本地环境

std::basic_ios<CharT,Traits>::imbue

std::locale imbue( const std::locale& loc );

 替换当前本地环境。等效地调用 ios_base::imbue(loc) ,而若有关联流缓冲( rdbuf() != 0 ),则再调用 rdbuf()->pubimbue(loc) 。

参数

loc-新的 locale

返回值

先前的 locale ,以 ios_base::imbue(loc) 返回。

异常

(无)

调用示例

#include <iostream>
#include <sstream>
#include <locale>

int main()
{
    std::istringstream iss;
    iss.imbue(std::locale("C"));

    std::cout << "Current locale: " << iss.getloc().name() << '\n';

    iss.imbue(std::locale());
    std::cout << "Global locale : " << iss.getloc().name() << '\n';
}

输出

管理相关的流缓冲区

std::basic_ios<CharT,Traits>::rdbuf

std::basic_streambuf<CharT, Traits>* rdbuf() const;

(1)

std::basic_streambuf<CharT, Traits>* rdbuf( std::basic_streambuf<CharT, Traits>* sb );

(2)

管理关联的流缓冲。

1) 返回关联的流缓冲。若无关联流缓冲,则返回空指针。

2) 设置关联流缓冲为 sb 。通过调用 clear() 清除错误状态。返回操作前的关联流缓冲。若无关联流缓冲,则返回空指针。

参数

sb-要关联的流缓冲

返回值

关联的流缓冲,或若无关联流缓冲则为空指针。

异常

(无)

调用示例

#include <iostream>
#include <sstream>

int main()
{
    std::ostringstream local;
    auto cout_buff = std::cout.rdbuf(); // 保存指向 std::cout 缓冲的指针

    std::cout.rdbuf(local.rdbuf()); // 以 'local' 对象的缓冲
    // 替换内部的 std::cout 缓冲

    // 现在 std::cout 以 'local' 缓冲工作
    // 你看不到此消息
    std::cout << "some message";

    // 回到旧缓冲
    std::cout.rdbuf(cout_buff);

    // 你将看到此消息
    std::cout << "back to default buffer\n";

    // 打印 'local' 内容
    std::cout << "local content: " << local.str() << "\n";
}

输出

 

管理绑定的流

std::basic_ios<CharT,Traits>::tie

std::basic_ostream<CharT,Traits>* tie() const;

(1)

std::basic_ostream<CharT,Traits>* tie( std::basic_ostream<CharT,Traits>* str );

(2)

 管理联系流。联系流是输出流,它与流缓冲( rdbuf() )所控制的输出序列同步,即在任何 *this 上的输入/输出操作前,在联系流上调用 flush() 。

1) 返回当前联系流。若无联系流,则返回空指针。

2) 设置当前联系流为 str 。返回操作前的联系流。若无联系流,则返回空指针。

参数

str-要设为联系流的输出流

返回值

联系流,或若无联系流则为空指针。

异常

(无)

注意

默认情况下,联系标准流 cincerrcout 。类似地,联系其宽对应版本 wcinwcerrwcout

调用示例

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ofstream os("test.txt");
    std::ifstream is("test.txt");
    std::string value("0");

    os << "Hello";
    is >> value;

    std::cout << "Result before tie(): \"" << value << "\"\n";
    is.clear();
    is.tie(&os);

    is >> value;

    std::cout << "Result after tie(): \"" << value << "\"\n";
}

输出


​​​​​​​窄化字符

std::basic_ios<CharT,Traits>::narrow

char narrow( char_type c, char dfault ) const;

 转换当前本地环境限定的字符 c 到其标准等价物。若需要则将结果从 char_type 转换到 char 。若不能进行转换,则函数返回 dfault

等效地调用 std::use_facet< std::ctype<char_type> >(getloc()).narrow(c, dfault); 。

参数

c-要转换的字符
dfault-若转化不成功则返回的字符

返回值

转换到标准等价物再到 char 的字符。若转换失败则返回 dfault

拓宽字符

std::basic_ios<CharT,Traits>::widen

char_type widen( char c ) const;

转换字符 c 到其在当前本地环境的等价物。若需要,则将结果从 char 转换为用于流内的字符类型。

等效地调用 std::use_facet< std::ctype<char_type> >(getloc()).widen(c) 。

参数

c-要转换的字符

返回值

转换为 char_type 的字符

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值