c++ 常用函数

#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 @ 2019-10-18 15:03  dangxusheng  阅读(403)  评论(0编辑  收藏  举报