8.c++根据年月日创建文件夹,根据时分秒创建txt文件

代码如下:

#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;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值