linux下c/c++ 检查是否有指定的文件夹,如果没有则创建创建文件夹

此文章只在linux验证通过,ubuntu18.04的ARM64架构(jetson Xavier )。

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
char folder_dir[128];
#folder_dir,自己初始化一下。
if((access(folder_dir, 0)) != -1)
{   
	printf("Dir exists\n");
}
else
{                           
	printf("not exist\n");
	mkdir(folder_dir,S_IRWXU);           
}

我的完整代码

#include <string.h>
#include <string>
#include <sstream>
#include <iostream>
#include <ostream>
#include <fstream>

#include <sys/time.h>

#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

time_t now = time(NULL);	
struct tm * timeinfo = localtime(&now); 
char timepath[60];
strftime(timepath, 60, "%Y_%m_%d_%H_%M_%S", timeinfo);		 
char time_ymd_path[30];
strftime(time_ymd_path, 30, "%Y_%m_%d", timeinfo);		
char file_name[256];
char folder_dir[128];
sprintf(folder_dir, "/home/xxxx/Desktop/work/%s", time_ymd_path);
if((access(folder_dir, 0)) != -1)
{   
	printf("Dir exists\n");
}
else
{                           
	printf("not exist\n");
	mkdir(folder_dir,S_IRWXU);           
}

sprintf(file_name, "%s/video%d_frame%03d_%s.jpg", folder_dir,
frame_meta->source_id, frame_meta->frame_num,timepath);

printf("[ALG][%s][%4d]time_ymd_path=%s,folder_dir=%s,file_name=%s\n",
__FUNCTION__, __LINE__,
time_ymd_path,
folder_dir,
file_name);

cv::imwrite(file_name, cvmat);

参考:
C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用

Logo

更多推荐