首页 > 其他 > 详细

在日志文件中输出当前时间

时间:2014-10-09 18:19:28      阅读:159      评论:0      收藏:0      [点我收藏+]

在代码中需要在出错的时候将错误写入到日志文件,而在写入错误时当然也需要将当前时间写入进去,下面的一段代码就是一个小实例。

 1 #include <iostream>
 2 #include <fstream>
 3 #include <ctime>
 4 
 5 using namespace std;
 6 
 7 int main(int argc, char **argv)
 8 {
 9     ofstream fout("test.log", ios::out | ios::app);
10     if(!fout.is_open())
11     {
12         cout << "Open log file failed" << endl;
13         return 0;
14     }
15 
16     // 写入日志
17     time_t timer;
18     struct tm *pstTime;
19     timer = time(NULL);
20     pstTime = localtime(&timer);
21     
22     fout << asctime(pstTime) << endl;
23     fout << "Errno : " << 3 << endl;
24     fout << "Error : " << "hh" << endl;
25     fout << endl << endl;
26 
27     return 0;
28 }

另附一段时间函数的简单用法代码

 1 #include <cstdio>
 2 #include <ctime>
 3 
 4 using namespace std;
 5 
 6 int main(int argc, char **argv)
 7 {
 8     time_t timer;
 9     struct tm *pstTime;
10 
11     timer = time(NULL);
12     pstTime = localtime(&timer);
13 
14     printf("Local time is:%s \n", asctime(pstTime));
15     printf("Local time is:%s \n", ctime(&timer));
16 
17     return 0;
18 }

 

在日志文件中输出当前时间

原文:http://www.cnblogs.com/lit10050528/p/4013865.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!