Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

1. 关键词

C++ 文件系统操作 查找指定文件夹下的特定文件 跨平台

2. fileutil.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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。

推荐阅读
C++文件系统操作5 - 跨平台列出指定目录下的所有文件和文件夹 C++文件系统操作5 - 跨平台列出指定目录下的所有文件和文件夹 C++文件系统操作1 - 跨平台实现文件的创建和删除 C++文件系统操作1 - 跨平台实现文件的创建和删除 C++文件系统操作4 - 跨平台实现获取文件|文件夹的大小 C++文件系统操作4 - 跨平台实现获取文件|文件夹的大小

评论