[2018年6月12号]C++ primer 课后练习 第十七章 标准库的特殊设施 第十八章用于大型程序的工具

17.37

17.38

    ifstream is("test_2.txt");
    char c[200];
    while (!is.eof())
    {
        is.getline(c, 200,'\n');
        cout.write(c, is.gcount());
    }

一开始的版本没有设置delim,发现getline无法自动前进到下一行,折腾了好久,当getline在读取一行发生错误时(没有遇到delim,或者读取文本大小大于给定的size)就不会前进到下一行,write也是一样(输出的内容小于或大于给定的size)

17.39    

fstream inOut("test_2.txt", fstream::ate|fstream::in|fstream::out);
    if(!inOut){
        return EXIT_FAILURE;
    }
    auto end_mark = inOut.tellg();
    inOut.seekg(0,fstream::beg);
    size_t cnt = 0;
    string line;
    while(inOut&&inOut.tellg() != end_mark&&getline(inOut,line)){
        cnt += line.size()+1;
        cout << line.size() << endl;
        auto mark = inOut.tellg();
        inOut.seekp(0,fstream::end);
        inOut << cnt;
        if(mark != end_mark)
        inOut << " ";
        inOut.seekg(mark);
    }
    inOut.seekp(0,fstream::end);
    inOut<< "\n";

18.1

a.range_error 对象

b.exception 指针 

会抛出一个指针而不是对象,无法捕获

18.2

v和in在异常发生后都被正确释放了,而指针p也被释放了,但是声请的int数组并没有被释放,造成内存泄露

18.3

1.将p创建为智能指针

2.将p构造成类,并在析构函数中释放声请的内存

18.4

越是继承链顶端越要写在前面

    try{
    }
    catch (overflow_error eobj) {

    }
    catch (const runtime_error & re) {
    }
    catch(exception){
    
    }
18.5


    try{
    }
    catch (const length_error& le) {
        cout << le.what();
        abort();
    }
    catch (const out_of_range& oor) {
        cout << oor.what();
        abort();
    }
    catch (const invalid_argument& ia) {
        cout << ia.what();
        abort();
    }
    catch (const domain_error& de) {
        cout << de.what();
        abort();
    }
    catch (const range_error& re) {
        cout << re.what();
        abort();
    }
    catch (const underflow_error& ue) {
        cout << ue.what();
        abort();
    }
    catch (const overflow_error& ue) {
        cout << ue.what();
        abort();
    }
    catch (const runtime_error & re) {
        cout << re.what();
        abort();
    }
    catch (const logic_error& ue) {
        cout << ue.what();
        abort();
    }
    catch (const bad_cast& bc) {
        cout << bc.what();
        abort();
    }
    catch (const bad_alloc& ba) {
        cout << ba.what();
        abort();
    }
    catch(const exception& e){
        cout << e.what();
        abort();
    }

18.6

a. throw &exceptionType();

b. throw  exception(); // 任何异常都能不火

c.throw EXCPTYPE;

18.7

    BlobPtr()try:curr(0){
    
    }catch(const std::bad_alloc & e ){
    }
    Blob(initializer_list<T> il)try{
    
    }catch(const std::bad_alloc& e){
    
    };
        
18.8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值