定时将系统时间更新在日志文件中

V1.0 2024年5月9日 发布于博客园

实现:设计程序,获取当前系统时间,把时间转换为特定格式”yy年mm月dd日 星期x tt:mm:ss”,并每隔1s写入到本地磁盘中一个叫做log.txt的文本中,如果文本不存在则创建。

代码

/**
 * @file name : writing_date.c
 * @brief     : 定时更新日志文件, 按ctrl + C结束程序
 * @author    : RISE_AND_GRIND@163.com
 * @date      : 2024/05/09
 * @version   : 1.0
 * @note      :设计程序,获取当前系统时间,把时间转换为特定格式”yy年mm月dd日 星期x tt:mm:ss”,
 *             并每隔1s写入到本地磁盘中一个叫做log.txt的文本中,如果文本不存在则创建。
 * CopyRight (c)  2023-2024   RISE_AND_GRIND@163.com   All Right Reseverd
 */
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>

int Print_Time(FILE *pfile);
int main(int argc, char const *argv[])
{

    // 打开文件
    FILE *logfile = fopen("log.txt", "a+b");
    if (NULL == logfile)
    {
        printf("Failed to open file!\n");
        exit(-1);
    }

    // 写入文件
    for (int i = 1; 1; ++i)
    {
        if (Print_Time(logfile) < 0)
        {
            printf("Failed to write time information to the file!\n");
            exit(-1);
        }
        sleep(1);
        printf("The log file is updated %d times\n", i);
    }

    // 关闭文件
    fclose(logfile);
    return 0;
}

/**
 * @name      Print_Time
 * @brief     将系统时间写入到文件中
 * @param     pfile 要写入的文件指针
 * @return
 *      @retval    负数 写入失败
 *      @retval    非负数 写入的字符总数
 * @date      2024/05/09
 * @version   1.0
 * @note
 */
int Print_Time(FILE *pfile)
{
    // 存放写入结果
    int Results = 0;

    // 存储日历时间, timer=NULL时得到当前日历时间(从1970-01-01 00:00:00到现在的秒数)
    time_t NowTimeSecs = time(NULL);

    // NowTimeTransition存放 转换timer的值为tm结构 后的值
    struct tm *NowTimeTransition = localtime(&NowTimeSecs);

    char *str = NULL; // 存放星期转换结果
    // 对转后的星期值进行汉字转换
    switch (NowTimeTransition->tm_wday)
    {
    case 1:
        str = "一";
        break;
    case 2:
        str = "二";
        break;
    case 3:
        str = "三";
        break;
    case 4:
        str = "四";
        break;
    case 5:
        str = "五";
        break;
    case 6:
        str = "六";
        break;
    case 0:
        str = "天";
        break;
    }

    // 发送格式化输出流到文件  格式为“yy年mm月dd日 星期x tt:mm:ss”
    Results = fprintf(pfile, "%d年%02d月%02d日 星期%s %02d:%02d:%02d \n",
                      NowTimeTransition->tm_year + 1900,
                      NowTimeTransition->tm_mon + 1,
                      NowTimeTransition->tm_mday,
                      str,
                      NowTimeTransition->tm_hour,
                      NowTimeTransition->tm_min,
                      NowTimeTransition->tm_sec);
    // 刷新全缓冲
    fflush(pfile);
    return Results;
}

测试

没有log.txt文件时会创建该文件,写入结果如下:

2024年05月10日 星期五 01:54:35 
2024年05月10日 星期五 01:54:36 
2024年05月10日 星期五 01:54:37 
2024年05月10日 星期五 01:54:38 
2024年05月10日 星期五 01:54:39 
2024年05月10日 星期五 01:54:40 
2024年05月10日 星期五 01:54:41 
2024年05月10日 星期五 01:54:42 
2024年05月10日 星期五 01:54:43 

控制台终端结果:

yuyi@IoTDevelopment:~/MyDevelopmentFile/homework/fileIO/20240509$ ./a.out 
The log file is updated 1 times
The log file is updated 2 times
The log file is updated 3 times
The log file is updated 4 times
^C
posted @ 2024-05-10 02:05  舟清颺  阅读(14)  评论(0编辑  收藏  举报