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

C++创建文件夹、判断是文件还是路径

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

C++创建文件夹、判断是文件还是路径

之前常在python里面用os模块处理路径,在C++里面没有那么顺手的方法,在这里封装几个常用的方法,创建文件夹,判断是不是文件,是不是路径。
注:makeDir创建单级文件夹(即上级目录已经存在的情况下),makeDirs创建多级文件夹(上级目录不存在则递归创建上级目录)

  • osProcess.h
#ifndef _OS_PROCESS_H_
#define _OS_PROCESS_H_

#include 
#include 
#include 

class osProcess
{
public:
	
	bool isDir(std::string path);

	
	bool isFile(std::string path);

	
	bool makeDir(std::string path);

	
	bool makeDirs(std::string path);

	
	std::string getFolderName(std::string path);
};

#endif //!_OS_PROCESS_H_
  • osProcess.cpp
#include "osProcess.h"

#include 


bool osProcess::isDir(std::string path)
{
	bool flg = false;
	struct stat s;
	if (stat(path.c_str(), &s) == 0)
	{
		if (s.st_mode & S_IFDIR)
			flg = true;
	}

	return flg;
}

bool osProcess::isFile(std::string path)
{
	bool flg = false;
	struct stat s;
	if (stat(path.c_str(), &s) == 0)
	{
		if (s.st_mode & S_IFREG)
			flg = true;
	}

	return flg;
}

bool osProcess::makeDir(std::string path)
{
	bool  flg = (_mkdir(path.c_str()) == 0);
	return flg;
}

bool osProcess::makeDirs(std::string path)
{
	bool flg = false;
	size_t index = path.find_last_of("\");
	std::string fartherDir = path.substr(0, index);
	if (!isDir(fartherDir))
	{
		flg = makeDirs(fartherDir);
	}
	flg = makeDir(path);

	return flg;
}

std::string osProcess::getFolderName(std::string path)
{
	std::string folderName;

	size_t index = path.find_last_of("\");
	std::string dir = path.substr(0, index);
	index = dir.find_last_of("\");
	folderName = dir.substr(index + 1, -1);

	return folderName;
}

void test_osProcess()
{
	std::string dir = "K:\imageData\OCR\ocr_dataset\test";
	std::string makeDir = "K:\imageData\OCR\ocr_dataset\test2";
	std::string makeDirs = "K:\imageData\OCR\ocr_dataset\test3\child\cat";
	std::string filePath = "K:\imageData\OCR\ocr_dataset\test\ablend_43.jpg";

	osProcess os;
	if (os.isDir(dir))
		std::cout << dir << " is dirn";
	else
		std::cout << dir << " is not dirn";

	if (os.isDir(filePath))
		std::cout << filePath << " is dirn";
	else
		std::cout << filePath << " is not dirn";

	if (os.isFile(filePath))
		std::cout << filePath << " is filen";
	else
		std::cout << filePath << " is not filen";

	if (os.isFile(dir))
		std::cout << dir << " is filen";
	else
		std::cout << dir << " is not filen";

	std::cout << filePath << "'s folderName is " << os.getFolderName(filePath) << std::endl;

	std::cout << "t TEST MAKEDIRn";
	std::cout << makeDir << " is exists ?" << os.isDir(makeDir) << std::endl;
	std::cout << "makedir" << os.makeDir(makeDir) << std::endl;
	std::cout << makeDir << " is exists ?" << os.isDir(makeDir) << std::endl;

	std::cout << "nt TEST MAKEDIRsn";
	std::cout << makeDirs << " is exists ?" << os.isDir(makeDirs) << std::endl;
	std::cout << "makedir" << os.makeDirs(makeDirs) << std::endl;
	std::cout << makeDirs << " is exists ?" << os.isDir(makeDirs) << std::endl;

}

int main()
{
	test_osProcess();
	return 0;
}

输出

K:imageDataOCRocr_datasettest is dir
K:imageDataOCRocr_datasettestablend_43.jpg is not dir
K:imageDataOCRocr_datasettestablend_43.jpg is file
K:imageDataOCRocr_datasettest is not file
K:imageDataOCRocr_datasettestablend_43.jpg's folderName is test
         TEST MAKEDIR
K:imageDataOCRocr_datasettest2 is exists ?0
makedir 1
K:imageDataOCRocr_datasettest2 is exists ?1

         TEST MAKEDIRs
K:imageDataOCRocr_datasettest3childcat is exists ?0
makedirs 1
K:imageDataOCRocr_datasettest3childcat is exists ?1

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

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

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