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