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

这篇博客介绍了如何在C++中封装类似Python的os模块功能,包括判断路径是否为目录、是否为文件、创建单级及多级目录,并提供了获取文件夹名称的方法。示例代码展示了类osProcess的实现及其测试用例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  • osProcess.h
#ifndef _OS_PROCESS_H_
#define _OS_PROCESS_H_

#include <sys/stat.h>
#include <direct.h>
#include <string>

class osProcess
{
public:
	/*
	判断path是不是一个目录
	*/
	bool isDir(std::string path);

	/*
	判断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 <iostream>


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 dir\n";
	else
		std::cout << dir << " is not dir\n";

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

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

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

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

	std::cout << "\t TEST MAKEDIR\n";
	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 << "\n\t TEST MAKEDIRs\n";
	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:\imageData\OCR\ocr_dataset\test is dir
K:\imageData\OCR\ocr_dataset\test\ablend_43.jpg is not dir
K:\imageData\OCR\ocr_dataset\test\ablend_43.jpg is file
K:\imageData\OCR\ocr_dataset\test is not file
K:\imageData\OCR\ocr_dataset\test\ablend_43.jpg's folderName is test
         TEST MAKEDIR
K:\imageData\OCR\ocr_dataset\test2 is exists ?0
makedir 1
K:\imageData\OCR\ocr_dataset\test2 is exists ?1

         TEST MAKEDIRs
K:\imageData\OCR\ocr_dataset\test3\child\cat is exists ?0
makedirs 1
K:\imageData\OCR\ocr_dataset\test3\child\cat is exists ?1

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值