0%

C++文件及文件夹操作(路径拼接/存在性/创建文件(夹)/读取目录文件列表)

  1. 文件(夹)是否存在
  2. 创建文件夹
  3. 路径拼接
  4. 读取目录下所有指定后缀的文件
  5. 修改/设定文件的后缀名
1
2
3
4
5
6
7
8
9
10
11
#pragma warning(disable:4996)  // 关闭_CRT_SECURE_NO_WARNINGS
#include <string>
#include <iostream>
#include <vector>
#include <io.h>
#include <direct.h>
#include <regex>
#include <initializer_list>
#include <cassert>
#include <fstream>
#include <algorithm>

文件(夹)是否存在

1
2
bool isFileExist(const std::string& filePath);
bool isDirExist(const std::string& dirPath);
1
2
3
4
5
6
7
8
9
10
bool FileHelper::isDirExist(const std::string& dirPath)
{
return _access(dirPath.c_str(), 0) != -1;
}

bool FileHelper::isFileExist(const std::string& filePath)
{
std::ifstream f(filePath.c_str());
return f.good();
}

创建文件夹

1
bool makeDir(const std::string& dirPath);
1
2
3
4
bool FileHelper::makeDir(const std::string& dirPath)
{
return _mkdir(dirPath.c_str()) == 0;
}

路径拼接

1
2
3
4
5
6
7
/// <summary>
/// 拼接路径
/// </summary>
/// <param name="pathList">路径列表 pathList = {"D:\\codes", "C++", "2020年12月31日"}</param>
/// <param name="joinTag">拼接符,"/"或"\\"</param>
/// <returns>拼接后的路径字符串 D:\\codes/C++/2020年12月31日</returns>
std::string join(std::initializer_list<std::string> pathList, std::string joinTag = "/");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string FileHelper::join(std::initializer_list<std::string> pathList, std::string joinTag)
{
if (pathList.size() == 0)
{
return std::string();
}
auto it = pathList.begin();
std::string fullPath(*it);
for (it++; it < pathList.end(); it++)
{
fullPath += joinTag;
fullPath += *it;
}
return fullPath;
}

读取目录下所有指定后缀的文件

1
2
3
4
5
/* 根据指针长度获取操作系统位数 */
int os_bit();

// 读取文件夹下的图片名称列表
bool readFileNamesFromDir(const std::string& dirPath, std::vector<std::string>& imgNames, const std::string& fileFilters = "\\.(jpg|png|bmp|jpeg|tif|raw|gif)$");
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
int FileHelper::os_bit()
{
return sizeof(int*) * 8;
}

bool FileHelper::readFileNamesFromDir(const std::string& dirPath, std::vector<std::string>& imgNames, const std::string& fileFilters)
{
if (FileHelper::os_bit() == 64)
{
__finddata64_t fileinfo;
__int64 hFile = 0;
if ((hFile = _findfirst64((dirPath + "/*").c_str(), &fileinfo)) != -1)
{
do {
// 32文件,16文件夹
if (fileinfo.attrib == 32)
{
// cout << fileinfo.name << endl;
std::regex img_reg(fileFilters);
std::smatch img_res;
std::string tmpFileName = std::string(fileinfo.name);
std::transform(tmpFileName.begin(), tmpFileName.end(), tmpFileName.begin(), ::tolower);
if (regex_search(tmpFileName, img_res, img_reg))
{
imgNames.push_back(fileinfo.name);
}
}
} while (_findnext64(hFile, &fileinfo) == 0);
_findclose(hFile);
}
else return false;
}
else
{
_finddata32_t fileinfo;
__int32 hFile = 0; // 文件句柄
if ((hFile = static_cast<__int32>(_findfirst32((dirPath + "/*").c_str(), &fileinfo)) != -1))
{
do {
// 32文件,16文件夹
if (fileinfo.attrib == 32)
{
// cout << fileinfo.name << endl;
std::regex img_reg(fileFilters);
std::smatch img_res;
std::string tmpFileName = std::string(fileinfo.name);
std::transform(tmpFileName.begin(), tmpFileName.end(), tmpFileName.begin(), ::tolower);
if (regex_search(tmpFileName, img_res, img_reg))
{
imgNames.push_back(fileinfo.name);
}
}
} while (_findnext32(hFile, &fileinfo) == 0);
_findclose(hFile);
}
else return false;
}
return true;
}

修改/设定文件的后缀名

1
2
3
4
5
6
7
/// <summary>
/// 设置文件名的后缀。输入 filename.jpg 或 filename,suffix 为 png,输出为 filename.png
/// </summary>
/// <param name="fileName">文件名,可带后缀或不带</param>
/// <param name="suffix">后缀</param>
/// <returns>带上指定后缀的文件名</returns>
std::string setSuffix(std::string fileName, std::string suffix);
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
std::string FileHelper::setSuffix(std::string fileName, std::string suffix)
{
try
{
if (suffix.size() == 0) throw "后缀长度为0";

if (FileHelper::isCorrectSuffix(fileName, suffix))
{
return fileName;
}

std::smatch suffixDetectResult = std::smatch();
if (std::regex_match(fileName, suffixDetectResult, std::regex("(.*)\\.(.*)$")))
{
return suffixDetectResult[1].str() + "." + suffix;
}
else
{
return fileName + "." + suffix;
}
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
return fileName;
}
}