您的位置:首页 > 编程语言 > C语言/C++

【C++学习】之如何用系统日期时间给文件命名

2016-08-27 23:14 375 查看
参考的网上的模板,直接给出程序:

#include "ctime"
#include "time.h"
using namespace std;
string int2string(int value)
{
stringstream ss;
ss<<value;
return ss.str();
}
time_t t=std::time(0);
struct tm * now = std::localtime( & t );
string file_name;
//the name of bag file is better to be determined by the system time
file_name=int2string(now->tm_year + 1900)+
'-'+int2string(now->tm_mon + 1)+
'-'+int2string(now->tm_mday)+
'-'+int2string(now->tm_hour)+
'-'+int2string(now->tm_min)+
'-'+int2string(now->tm_sec)+
".bag";


整个程序在我的上一篇博客中有,可以看下。如果你是在ubuntu,ros环境下搞的话,这里面需要注意的是在time(0)和localtime()前面加上std::,可能是因为ubuntu或者ros下对这两个函数有重定义,不加的话编译会报错。

当然这里面也涉及到了把int类型转换成string类型,我使用的是stringstream
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: