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

文件相关 *** 作和字符串等备忘,第1张

文件相关 *** 作和字符串等备忘 std::wstring 和 std::string 互相转换
#include 
#include 
#include 

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

// convert wstring to string 
inline std::string to_byte_string(const std::wstring& input)
{
	std::wstring_convert> converter;
	return converter.to_bytes(input);
}
创建文件目录
#include 
#include 

//创建路径 -- mkpath("log/2021");
void mkpath(const std::string& path)
{
	wchar_t buf[260];
	std::wstring_convert> 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& 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& 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(tmpInfo.st_ctime);
	iModifyTime = static_cast(tmpInfo.st_mtime);
	iAccessTime = static_cast(tmpInfo.st_atime);
	iFileLen = static_cast(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);
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5657459.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存