【问题标题】:For Loop not reading ifstreamFor循环不读取ifstream
【发布时间】:2020-05-24 00:26:54
【问题描述】:

我正在尝试读取文件夹中的多个文件,以便解析它们的数据。 我首先尝试使用包含所有文件名的文本文档来填充列表,然后基于该字符串向量,不断调用 ifstream 以便我可以读取每个文件并处理单词数据。

我遇到的问题是 ifstream 无法打开所有文件,除了列表中间的一个?
Heres the output, its failing to read the dbfiles but they all have the right names?

这些文件不超过 8GB,所以它应该能够处理它,但不是吗? 可能是文件路径有问题?

 std::ifstream dbfiles(argv[1]);
    if (!dbfiles)
    {
        std::cerr << "Failed to open database " << argv[1] << " for reading." << std::endl;
    }


    std::string word;
    std::vector<std::string> dbfile_names;
    std::string file_name;

    while (getline(dbfiles, file_name))
    { //reading in the file names
        dbfile_names.push_back(file_name);
    }//populate list of dbs

    dbfiles.close();

    for (unsigned int j = 0; j < dbfile_names.size(); j++)
    { //for every single file

    std::ifstream dbfile(dbfile_names[j].c_str());
    if (!dbfile)
    {
        std::cout << "Failed to open database file" <<  dbfile_names[j] << " for reading. READ FAILURE" << std::endl;
    }else{
        std::cout << "currently reading " << dbfile_names[j] << std::endl;
    }

        while (dbfile >> word)
        { 
           //do stuff with the segments of data
           //here I gather the data word by word and process it
        }
        dbfile.close();
   }

【问题讨论】:

    标签: c++ for-loop ifstream


    【解决方案1】:

    我进入调试器,发现由于getline,所有文件名的后面都有一个/r。 此处Getting std :: ifstream to handle LF, CR, and CRLF? 的帖子帮助描述了问题以及如何轻松解决它。

    我的文件现在正在相应地读取

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 2013-08-28
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      相关资源
      最近更新 更多