代码如下:
#include <iostream>
#include<io.h>
#include<direct.h>
#include<time.h>
#include <cstdio>
#include <fstream>
#include <string.h>
using namespace std;
int mkfiles()
{
cout << "......................开始创建文件夹......................" << endl;
//std::string savepath = ".\\test2\\result\\";
//自动创建文件夹并保存结果
struct tm t;
time_t now; //声明time_t类型变量
time(&now); //获取系统日期和时间
localtime_s(&t, &now); //获取当地日期和时间
char cNowTime[64];
char chmsTime[64];
strftime(cNowTime, sizeof(cNowTime), "%Y_%m_%d", &t);
strftime(chmsTime, sizeof(chmsTime), "%H_%M_%S", &t);
cout << cNowTime << endl;
cout << chmsTime << endl;
std::string timedir1;
std::string timedir2;
timedir1 = cNowTime;
timedir2 = chmsTime;
string savepath=(".\\" + timedir1).c_str();
if (_access(savepath.c_str(), 0) == -1)
{
_mkdir(savepath.c_str());
}
//生成txt文件
string savepaths = savepath + "\\" + timedir2 + ".txt";
const char* p = savepaths .data();
// 打开文件并创建文件指针
ofstream write_txt;
// 打开文件
write_txt.open(p);
// 判断文件是否打开
if (!write_txt.is_open())
{
cout << "file open is error!" << endl;
return 0;
}
// 将字符串写入文件中
write_txt << "words" << endl;
write_txt << "words" << endl;
// 关闭文件流
write_txt.close();
return 0;
}