c++新建文件夹和txt文件,并写入数据

首先包含头文件

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值