【问题标题】:Open all text files in given folder打开给定文件夹中的所有文本文件
【发布时间】:2014-05-18 10:52:59
【问题描述】:

我试图打开一个文件夹和子文件夹中的所有文本文件,我作为参数提供给程序并在其中搜索文本。现在,如果我使用 .它不是路径,而是像我想要的那样打开文件。但是,一旦我将计算机中的任何其他文件夹作为参数(不是目标文件所在的文件夹),它就不会打开文件。我该如何解决?我有 Windows,我使用 MinGw 作为编译器。

#include <iostream>
#include <cstdlib>
#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"
#include <iterator>
#include <fstream> 
namespace po = boost::program_options;
using namespace std;
using namespace boost;
using namespace boost::filesystem;



int main (int argc, char* argv[]) {

    // Declare the supported options.
    po::options_description desc("Allowed options");
    desc.add_options()
    ("folder", po::value<std::string>(), "find files in this folder")
    ("text", po::value<std::string>(), "text that will be searched for");

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    filesystem::path current_dir (vm["folder"].as<string>());
    filesystem:: recursive_directory_iterator end_itr;//recursive lisatud

    for ( filesystem::recursive_directory_iterator itr( current_dir );
         itr != end_itr;
         ++itr )
    {
        //cout << itr->path ().filename () << endl;
        if(itr->path().filename().extension()==".txt"||itr->path().filename().extension()==".docx"||itr->path().filename().extension()==".doc"){
            ifstream inFile(itr->path().filename().string());
            //ifstream inFile("c:\\somefile.txt"); //this would open file

            cout<<itr->path().filename().string()<<endl; //this prints out all the file names without path, like  somefile2.txt for example
            while ( inFile )
            {
                std::string s;
                std::getline( inFile, s );
                if (inFile){
                    std::cout << s << "\n";
                    if(s.find(vm["text"].as<string>())!= string::npos){
                        cout<<"found it"<<endl;
                        }
                    }
            } 
        }
    }


    return EXIT_SUCCESS;
}

【问题讨论】:

  • 它不会打开文件”不是一个非常有用的问题描述,并且“一个文件夹”和“任何其他文件夹”不是对您的测试数据非常有用的描述, 任何一个。也许你可以详细说明?
  • "它不会打开文件" - 它不会进入循环 while(inFile){} - 一个文件夹,以及任何其他文件夹 - 一个文件夹是我给程序的路径,它可以是我电脑中的任意路径,其他文件夹是该文件夹的子文件夹
  • 您应该检查itr-&gt;path().filename() 的值。因为如果文件的部分是some/directory/foo.txt,但.filename() 只是返回foo.txt,您将无法打开它,因为路径丢失。当然,使用其他文件系统库很容易犯错误。
  • 你是正确的 .filename() 只返回 foo.txt 没有路径。而且我相信这是我遇到的主要问题,但我怎样才能获得整个路径?

标签: c++ boost


【解决方案1】:

问题来了:

ifstream inFile(itr->path().filename().string())

或更具体地说,itr-&gt;path().filename() 仅返回文件的名称,而不是该文件的完整路径。如果文件不在程序的当前工作目录中,则打开它时会遇到问题:要么找不到该名称的文件,要么会找到同名的本地文件,而该文件不是该文件你真的想要!

当您执行递归目录迭代时,您当前的工作目录不会改变。

itr-&gt;path() 在取消引用时返回boost::filesystem::path 的实例...该类的文档可以在here 中找到。我相信你想做的是

ifstream inFile(itr->path().c_str());

但是,这可能不是规范或最有效的方法。

【讨论】:

  • native 可以很好地获取整个路径,但是 ifstream inFile(itr-> path().native()) 给我一个错误:没有匹配函数调用 'std::basic_ifstream :: basic_ifstream(const string_tyoe&)。在行尾添加 .string() 也不起作用。
  • 我的错。您可以使用 itr-&gt;path().c_str() 来获得一个适合您的普通旧 C 字符串版本。答案已更新。
  • 我不知道我是否犯了一些完全的菜鸟错误,但它仍然不起作用。报错基本一样没有匹配函数,只是结尾改成了(const value:type*)
  • 恐怕我的想法已经用完了。我手边没有任何可以使用 boost 的机器。也许itr-&gt;path().string()itr-&gt;path().string().c_str()?如果这不起作用,我只能建议您自己阅读文档,因为本周末我无法提供任何更具体的帮助;-) 我注意到 VS2013 和 GCC 4.7 都乐于构建 @ 987654330@ 来自 std::string 路径。
猜你喜欢
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多