c++写入txt以及获取日期

上代码

#include<iostream>
#include<fstream>
#include<string>
#include<time.h>

using namespace std;

void write_txt(ofstream& outfile) {
	outfile << to_string(40) + "度\n";
	outfile << "ctr_pt1: 0.001\n";
	outfile << "ctr_pt2: -0.51\n";
}

void get_nowtime_fix(string& prefix) {
	time_t now_time;
	struct tm* p = new tm;
	time(&now_time);
	localtime_s(p, &now_time);
	int year = p->tm_year + 1900, month = p->tm_mon + 1, day = p->tm_mday;
	int hour = p->tm_hour, minute = p->tm_min, second = p->tm_sec;
	delete p;

	prefix = to_string(year) + string(2 - to_string(month).length(), '0') + to_string(month)
		+ string(2 - to_string(day).length(), '0') + to_string(day) + "_"
		+ string(2 - to_string(hour).length(), '0') + to_string(hour)
		+ string(2 - to_string(minute).length(), '0') + to_string(minute)
		+ string(2 - to_string(second).length(), '0') + to_string(second);
}

int main() {
	
	// cout << year <<month<< endl;
	string prefix;
	get_nowtime_fix(prefix);
	cout << prefix << endl;

	ofstream outfile(prefix+"in_OffPlane.txt", ios::trunc); // 新建并清空
	write_txt(outfile);
	
	outfile.close();
	return 0;
}

会在本地项目文件夹下生成以相应的时间作为前缀的文件名.txt

结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值