在一些圖像處理或者數據處理時,需要給計算返回的數據指定一個存儲路徑,如果直接以被處理的文件所在路徑下,並以其名命名自動生成一個文件夾,挺方便的,做法如下;
CreateSubfolder.h
/* create subfolder for results saving.
- Email: [email protected]
- Address: Computer Vision and Remote Sensing(CVRS) Lab, Wuhan University.
*/
#ifndef CREATE_SUBFOLDER_H
#define CREATE_SUBFOLDER_H
#pragma once
#include
#include
#include
#include
using namespace std;
class CreateSubfolder
{
public:
CreateSubfolder(){}
~CreateSubfolder(){}
public:
/*
Create a subfolder according to the full-path of the input filepathh
- filepath: full path, input
- subfolder: created folder, output
- filename: name of the input file, output
*/
static void creatSubfolderViaFname(
char* filepath, char* subfolder, char* filename );
};
#endif // CREATE_SUBFOLDER_H
CreateSubfolder.cpp
#include "CreateSubfolder.h"
void CreateSubfolder::creatSubfolderViaFname(
char* filepath, char* subfolder, char* filename )
{
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
_splitpath(filepath, drive, dir, fname, ext);
char* subDir = new char[_MAX_DIR];
sprintf(subDir, "%s\%s\\", dir, fname);
sprintf(subfolder, "%s\%s", drive, subDir);
_mkdir(subfolder);
sprintf(filename, "%s", fname);
delete[] subDir;
}
比如,下面的例子,對一張圖片摳出每個單獨的圖形,並存在該文件夾下:
運行完就是這效果:
(圖像處理的代碼很簡單,就不公布啦~)