栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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);
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/655370.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号