zoukankan      html  css  js  c++  java
  • 在日志文件中输出当前时间

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

     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 
    ", asctime(pstTime));
    15     printf("Local time is:%s 
    ", ctime(&timer));
    16 
    17     return 0;
    18 }
  • 相关阅读:
    HDU-3622 Bomb Game 2sat
    HDU-4115 Eliminate the Conflict 2sat
    POJ-3678 Katu Puzzle 2sat
    [转]2-SAT问题及其算法
    Ros学习——roslaunch
    Ros学习调试——rqt_console
    Ros学习——创建程序包
    Ros学习service——小海龟
    Ros学习topic——小海龟
    旋转矩阵相关变换
  • 原文地址:https://www.cnblogs.com/lit10050528/p/4013865.html
Copyright © 2011-2022 走看看