c++ 常用函数

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#pragma once
 
#include <iostream> 
#include <io.h>
#include <direct.h>
#include <time.h>
#include <string>
#include <vector>
 
#include <windows.h>
 
using namespace std;
 
//读取某给定路径下所有文件夹与文件名称,并带完整路径
static void getAllFilesInDirs(string path, vector<string>& fileFullpath) {
 
    _finddata_t fileInfo;
    //将string转化为const char*类型
    string path1 = path + "\\*.*";
    intptr_t fileHandle = _findfirst(path1.c_str(), &fileInfo);
    //读取第一个文件信息 文件句柄类型为long会出现不兼容[Win10平台用long声明文件句柄会crash]
    string f;
    if (fileHandle == -1) {
        cout << "error\n";
        return;
    }
    while (_findnext(fileHandle, &fileInfo) == 0) {
 
        if (fileInfo.attrib & _A_SUBDIR) {
            if (strcmp(fileInfo.name, ".") == 0 || strcmp(fileInfo.name, "..") == 0)  continue;
            string newPath = path + "\\" + fileInfo.name;
            getAllFilesInDirs(newPath, fileFullpath);
        }
        else {
            f = path + string("\\") + string(fileInfo.name);
            fileFullpath.push_back(f);
        }
    };
    _findclose(fileHandle);
}
 
 
// 获取文件后缀名
static string getExtension(string filename) {
    return filename.substr(filename.find_last_of('.'));
}
 
// 获取文件名,不包含目录
static string getFilename(string file_fullpath) {
    return file_fullpath.substr(file_fullpath.find_last_of('\\') + 1);
}
 
//获取文件名,不包含目录和后缀名
static string getFilenameNoExt(string filename) {
    filename = getFilename(filename);
    return filename.substr(0, filename.find_last_of('.'));
}
 
 
////读取某给定路径下后缀名为format得文件名称,并带完整路径
static void getAllFilesByformat(string path, vector<string>& fileFullpath, string format) {
    _finddata_t fileInfo;
    //将string转化为const char*类型
    string path1 = path + "\\*.*";
    intptr_t fileHandle = _findfirst(path1.c_str(), &fileInfo);
    //读取第一个文件信息 文件句柄类型为long会出现不兼容[Win10平台用long声明文件句柄会crash]
    string f;
    if (fileHandle == -1) {
        cout << "error\n";
        return;
    }
    while (_findnext(fileHandle, &fileInfo) == 0) {
 
        if (fileInfo.attrib & _A_SUBDIR) {
            if (strcmp(fileInfo.name, ".") == 0 || strcmp(fileInfo.name, "..") == 0)  continue;
            string newPath = path + "\\" + fileInfo.name;
            getAllFilesByformat(newPath, fileFullpath, format);
        }
        else {
            if (getExtension(fileInfo.name) == format)
            {
                f.clear();
                f = path + string("\\") + string(fileInfo.name);
                fileFullpath.push_back(f);
            }
        }
    };
    _findclose(fileHandle);
}
 
 
 
// 字符串分割到 vector
static vector<string> string_split(const string& str, const string& delim) {
    vector<string> res;
    if ("" == str) return res;
    //先将要切割的字符串从string类型转换为char*类型 
    char * strs = new char[str.length() + 1]; //不要忘了 
    strcpy(strs, str.c_str());
 
    char * d = new char[delim.length() + 1];
    strcpy(d, delim.c_str());
 
    char *p = strtok(strs, d);
    while (p) {
        string s = p; //分割得到的字符串转换为string类型 
        res.push_back(s); //存入结果数组 
        p = strtok(NULL, d);
    }
    return res;
}
 
 
// 递归创建文件夹
static int self_mkdirs(string filename) {
    filename += "\\";
    char *fileName = (char*)filename.c_str();
    char *tag;
    int a = 0;
    for (tag = fileName; *tag; tag++, a++)
    {
        if (*tag == '\\')
        {
            string new_path = filename.substr(0, a + 1);
 
            if (_access(new_path.c_str(), 0) == -1) //如果文件夹不存在
                _mkdir(new_path.c_str());               //则创建
        }
    }
 
    return 0;
}
 
// 文件是否存在
static bool exist_file(string filename) {
    return _access(filename.c_str(), 4) == 0;
}
 
// 打乱vector
static vector<string> vector_shuffle(vector<string> v1) {
    vector<string> v2;
    // 复制
    v2.assign(v1.begin(), v1.end());
    //打乱
    random_shuffle(v2.begin(), v2.end());
 
    return v2;
}
 
 
///替换指定字符串
static string  replace_all(string str, const string& old_value, const string& new_value)
{
    string str1 = "";
    while (true) {
        string::size_type   pos(0);
        if ((pos = str.find(old_value)) != string::npos)
            str1 = str.replace(pos, old_value.length(), new_value);
        else   break;
    }
    return   str1;
}
 
 
static std::string GetTime() {
    time_t timep;
    time(&timep);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&timep));
    return tmp;
}

  

posted @   dangxusheng  阅读(404)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示