1. 关键词
C++ 文件系统操作 查找指定文件夹下的特定文件 跨平台
2. fileutil.h
#pragma once
#include <string>
#include <cstdio>
#include <cstdint>
#include "filetype.h"
#include "filepath.h"
namespace cutl
{
/**
* @brief The file guard class to manage the FILE pointer automatically.
* file_guard object can close the FILE pointer automatically when his scope is exit.
*/
class file_guard
{
public:
/**
* @brief Construct a new file guard object
*
* @param file the pointer of the FILE object
*/
explicit file_guard(FILE *file);
/**
* @brief Destroy the file guard object
*
*/
~file_guard();
/**
* @brief Get the FILE pointer.
*
* @return FILE*
*/
FILE *getfd() const;
private:
FILE *file_;
};
/**
* @brief Find all files in a directory by name.
*
* @param dirpath the filepath of the directory to be searched
* @param name the name of the file to be found, substring match is supported.
* @param recursive whether to search the whole directory recursively, default is false.
* If true, means search the whole directory recursively, like the 'find' command.
* @return filevec the vector of file_entity, filepaths in the directory.
*/
filevec find_files(const filepath &dirpath, const std::string &name, bool recursive = false);
/**
* @brief Find all files in a directory by extension.
*
* @param dirpath the filepath of the directory to be searched
* @param extension the extension of the file to be found, with dot, like ".txt".
* @param recursive whether to search the whole directory recursively, default is false.
* If true, means search the whole directory recursively, like the 'find' command.
* @return filevec the vector of file_entity, filepaths in the directory.
*/
filevec find_files_by_extension(const filepath &dirpath, const std::string &extension, bool recursive = false);
} // namespace cutl
3. fileutil.cpp
#include <cstdio>
#include <map>
#include <iostream>
#include <cstring>
#include <sys/stat.h>
#include "fileutil.h"
#include "inner/logger.h"
#include "inner/filesystem.h"
#include "strutil.h"
namespace cutl
{
file_guard::file_guard(FILE *file)
: file_(file)
{
}
file_guard::~file_guard()
{
if (file_)
{
// CUTL_DEBUG("close file");
int ret = fclose(file_);
if (ret != 0)
{
CUTL_ERROR("fail to close file, ret" + std::to_string(ret));
}
file_ = nullptr;
}
// ROBOLOG_DCHECK(file_ == nullptr);
}
FILE *file_guard::getfd() const
{
return file_;
}
filevec find_files(const filepath &dirpath, const std::string &name, bool recursive)
{
filevec filelist = list_files(dirpath, filetype::all, recursive);
filevec result;
for (auto &file : filelist)
{
if (file.type == filetype::directory)
{
continue;
}
auto filename = cutl::path(file.filepath).basename();
if (filename.find(name) != std::string::npos)
{
result.emplace_back(file);
}
}
return result;
}
filevec find_files_by_extension(const filepath &dirpath, const std::string &extension, bool recursive)
{
filevec filelist = list_files(dirpath, filetype::all, recursive);
filevec result;
for (auto &file : filelist)
{
if (file.type == filetype::directory)
{
continue;
}
auto extname = cutl::path(file.filepath).extension();
if (cutl::to_lower(extname) == cutl::to_lower(extension))
{
result.emplace_back(file);
}
}
return result;
}
4. list_files
list_files
函数的定义参见: http://sunlogging.com/2024/07/25/cpp_common_util/fileutil_list_files/
5. 源码地址
更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。
【SunLogging】
扫码二维码,关注微信公众号,精彩内容