【问题标题】:Is there any way to check whether a file exist with a file name contains regular expression有什么方法可以检查文件是否存在,文件名包含正则表达式
【发布时间】:2013-10-05 00:10:37
【问题描述】:

给定您要搜索的路径,并且文件名包含正则表达式,有没有办法检查该路径下是否存在满足文件名模式的文件?在 C++ 中??例如: 路径下

/path

我想搜索文件“foo.*”

foo.* 

结果可能是 foo.1 或 foo.2 或其他。我想得到结果(我的意思是我想知道文件是否存在以及确切的文件名),谢谢

【问题讨论】:

  • 您要查找的搜索词是"glob"。在 Linux-y 系统上,请参阅 glob。在 Windows 上,FindFirstFile 等人。
  • 您的意思是 foo 后接任何内容(正则表达式)还是 foo 后接点后接任何内容(glob)?
  • 请记住,任何此类问题的答案一经获得即已过时 - 如果创建了新文件怎么办?
  • @IgorTandetnik 谢谢,“glob”似乎是我正在寻找的东西

标签: c++ path file-exists


【解决方案1】:

查看boost::filesystem,否则你需要查看你的操作系统的api。

这是一个例子(未经测试):

boost::filesystem::path dir("/path");
boost::filesystem::directory_iterator it(dir), end;
BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(it, end))
{
    if(boost::filesystem::is_regular_file(p))
    {
        if(p.stem().string() == "foo")
        {
            std::cout << p.extension() << '\n';
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多