C++文件夹操作之自动创建

C++文件夹操作之自动创建

1.读取系统时间自动创建

  • 头文件:
#include<io.h>
#include<direct.h>
#include<time.h>
  • 实现代码:
        std::string imgspath = ".\\test2\\jpgs";
		std::string savepath = ".\\test2\\result\\";

		//自动创建文件夹并保存结果
		struct tm t;
		time_t now;  //声明time_t类型变量
		time(&now);      //获取系统日期和时间
		localtime_s(&t, &now);   //获取当地日期和时间
		char cNowTime[64];
		strftime(cNowTime, sizeof(cNowTime), "%Y%m%d-%H%M%S", &t);
		std::string timedir;
		timedir = cNowTime;
		std::string dir = (savepath + timedir).c_str();
		if (_access(dir.c_str(), 0) == -1)
		{
    
    
			_mkdir(dir.c_str());
		}
		std::string dir_yes = dir + ".\\success";
		std::string dir_0 = dir + ".\\Error0";
		std::string dir_1 = dir + ".\\Error1";
		std::string dir_2 = dir + ".\\Error2";

		_mkdir(dir_yes.c_str());
		_mkdir(dir_0.c_str());
		_mkdir(dir_1.c_str());
		_mkdir(dir_2.c_str());

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/yohnyang/article/details/128916532