文件相关操作和字符串等备忘

std::wstring 和 std::string 互相转换

#include <string>
#include <locale>
#include <codecvt>

// convert string to wstring
inline std::wstring to_wide_string(const std::string& input)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
	return converter.from_bytes(input);
}

// convert wstring to string 
inline std::string to_byte_string(const std::wstring& input)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
	return converter.to_bytes(input);
}

创建文件目录

#include <string>
#include <accctrl.h>

//创建路径 -- mkpath("log/2021");
void mkpath(const std::string& path)
{
	wchar_t buf[260];
	std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
	DWORD retLen = GetFullPathName(converter.from_bytes(path).c_str(), 260, buf, nullptr);
	const bool result = ::CreateDirectory(buf, 0);
}

获取文件大小

size_t getFileSize(const std::string& file_name) {
	std::ifstream in(file_name.c_str());
	if (in)
	{
		in.seekg(0, std::ios::end);
		size_t size = in.tellg(); 
		in.close();
		return size; //单位是:byte
	}
	else
	{
		return 0;
	}
}

获取文件夹下所有文件名

//循环迭代式获取文件中的某种格式的文件,
//Path 路径 如 获取D:\\Test 目录下的所有文件*.png,    path = "D:\\Test",strFileType;    strFileType = "\\*.png"
// https://blog.csdn.net/xiexu911/article/details/79990774?utm_medium=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-1.test_version_3&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-1.test_version_
//strFileType = \\*; 取全部文件
//strFileType = \\*.png;
bool inline getFiles(string path, const char* strFileType, vector<string>& files)
{
	//文件句柄  	//文件信息 
	struct _finddata_t fileinfo;
	string p;
	__int64   hFile = _findfirst(p.assign(path).append(strFileType).c_str(), &fileinfo);
	if (hFile != -1)
	{
		do
		{
			//如果是目录,迭代之  
			//如果不是,加入列表  
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(p.assign(path).append("\\").append(fileinfo.name), strFileType, files);
			}
			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name));
			}
		}while (_findnext(hFile, &fileinfo) == 0);

		_findclose(hFile);
	}
	return true;
}

获取指定目录下的文件夹名


void getFolders(string path, vector<string>& files)
{
	struct _finddata_t fileinfo;
	string p;
	__int64   hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo);
	if (hFile != -1)
	{
		do
		{
			if ((fileinfo.attrib & _A_SUBDIR) && (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0))
			{
				files.push_back(fileinfo.name);
			}
		} while (_findnext(hFile, &fileinfo) == 0);

		_findclose(hFile);
	}
}

获取文件创建时间,修改时间,访问时间,文件长度

//c++ 获取文件创建时间、修改时间、访问时间、文件内容长度
bool GetFileInfo(string& strPath, int& iCreateTime, int& iModifyTime, int& iAccessTime, int& iFileLen)
{
	struct _stat tmpInfo;
	if (_stat(strPath.c_str(), &tmpInfo) != 0)
	{
		return false;
	}
	iCreateTime = static_cast<int>(tmpInfo.st_ctime);
	iModifyTime = static_cast<int>(tmpInfo.st_mtime);
	iAccessTime = static_cast<int>(tmpInfo.st_atime);
	iFileLen = static_cast<int>(tmpInfo.st_size);

	return true;
}

是否文件夹

bool isFolder(const string& filename)
{
	WIN32_FIND_DATAA FindFileData;
	FindFirstFileA(filename.c_str(), &FindFileData);
	return (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
}

文件是否存在

bool exists(const std::string& name)
{
	struct stat buffer;
	return (stat(name.c_str(), &buffer) == 0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值