首先包含头文件
#include<iostream>
#include<fstream>
#include<ctime>
#include<time.h>
#include<string>
using namespace std;
然后是主函数
int main()
{
// 读取当地时间
struct tm t; // 新建 tm结构体
time_t now; // 定义变量 now
time(&now);
localtime_s(&t,&now); // 使用 localtime_s 函数读取时间,不使用 localtime
cout<<"当地时间 是"<<t.tm_year+1900<<"-"<<t.tm_mon+1<<"-"<<t.tm_mday<<"_"<<t.tm_hour<<"_"<<t.tm_min<<"_"<<t.tm_sec<<endl;
// 接下来 就是根据这个时间来创建文件夹
string p0="mkdir ";// 注意这里有个空格,不能少
string p1=to_string(t.tm_year+1900)+"_"+to_string(t.tm_mon+1)+"_"+to_string(t.tm_mday)+"_"+to_string(t.tm_hour)+"_"+to_string(t.tm_min)+"_"+to_string(t.tm_sec);
string p2="C:\\Users\\Desktop\\"+p1; // 注意斜杠是两个。
string p3=p2+"\\test1";
string p4=p2+"\\test2";
system((p0+p2).c_str()); //产生以时间为名字的文件夹
system((p0+p3).c_str()); // 在此文件夹下面,再生成 test1和test2 子文件夹。
system((p0+p4).c_str());
// 在 test1下新建txt文件 ,并写入字符串
string p6=p3+"\\123.txt";
ofstream f;
f.open(p6,ios::out | ios::app);
f<<"hello"<<endl;
f.close();
return 0;
}