• read more files using only one std::ifstream file


    Today meets a bug, when read more than one files using only one std::ifstream object. possible code as following:

    std::ifstream infile
    
    infile.open(a)
    if (infile.bad()) {
       std::cerr << "read failed" << std::endl;  
    }
    
    std::string line = "";
    while (std::getline(infile, line), infile.good()) {
       std::cout << line << std::endl;
    }
    
    infile.close();
    
    //next ,continue read b file
    infile.open(b);
    
    if (infile.bad()) {
      std::cerr << "read failed" << std::endl;
    }
    line = "";
    while (std::getline(infile, line), infile.good()) {
       std::cout << line << std::endl;
    }

    but, file b can not be read out!

    OK, the bug is :after read out of file a, the object "infile"'s state is error, because of reading nothing out finally for file a.

    only use "infile.close()" is not enough, it also needs clear; so ,fix it as follows:

    infile.close()
    infile.clear();

    then bug fixed.

    reminding~

  • 相关阅读:
    DOM事件模型
    Javascript 跨域
    浏览器内核及差异
    对WEB标准的理解
    SVN权限解析规则详解
    一款成熟的前端框架——Bootstrap
    终于可以发文了
    一些感想
    Linux升级nodejs及多版本管理
    zepto 自定义build
  • 原文地址:https://www.cnblogs.com/crafet/p/2880603.html
Copyright © 2020-2023  润新知