之前常在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" #includebool 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



