简体   繁体   English

For循环不读取ifstream

[英]For Loop not reading ifstream

I'm trying to read multiple files in a folder so I can parse through their data.我正在尝试读取文件夹中的多个文件,以便解析它们的数据。 I first try to fill the list using a text document with all the file names in it, then based on that vector of string, continuously call ifstream so I can read every file and process the word data.我首先尝试使用包含所有文件名的文本文档来填充列表,然后基于该字符串向量,不断调用 ifstream 以便我可以读取每个文件并处理单词数据。

The problem I'm running into is that ifstream is failing to open all of the files, except one in the middle of the list?我遇到的问题是 ifstream 无法打开所有文件,除了列表中间的一个文件?
Heres the output, its failing to read the dbfiles but they all have the right names?继承人 output,它无法读取 dbfiles 但它们都有正确的名称?

These files aren't more than 8GB a piece so it should be able to handle it but it's not?这些文件不超过8GB,所以它应该能够处理它,但不是吗? maybe theres a problem with the file paths?可能是文件路径有问题?

 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();
   }

I went into my debugger and found that due to getline, all the file names had a /r at the back of them.我进入调试器,发现由于 getline,所有文件名的后面都有 /r。 The post over here Getting std:: ifstream to handle LF, CR, and CRLF?此处的帖子让 std::ifstream 处理 LF、CR 和 CRLF? , helped describe the problem and how to easily fix it. ,帮助描述了问题以及如何轻松解决问题。

My files are now reading accordingly我的文件现在正在相应地读取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM