上代码
#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
结果