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

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

/*******************************************************************
 *
 *	file name:	getime.c
 *	author	 :  Dazz
 *	date	 :  2024/5/8
 *	function :  设计程序,获取当前系统时间,把时间转换为特定格式”yy年mm月dd日星期x
 *               mmss”,并每隔1s写入到本地磁盘中一个叫做1og.txt的文本中,如果文本不存在则创建。
 * 	note	 :  None
 *
 *	CopyRight (c)  2024-202x   Dazz_24@163.com   All Right Reseverd
 *
 * *****************************************************************/
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{

    // 以只写的方式打开或创建文件"log.txt"
    FILE *file = fopen("log.txt", "wb");
	if(NULL == file)
	{
	printf("fopen file failed!\n");
	exit(1);
	}

    while (1)
    {
        // 获取当前当前的时间戳
        time_t timep = time(NULL);

        // 将时间戳的地址作为参数传递给函数localtime
        struct tm *timerow = localtime(&timep);

		int temp= 0;

		if(!timerow->tm_wday)
		{
		 temp = 7;
		}
		else
		{
		tmep = timerow->tm_wday
		}

        // 将数据输出到文件中
        fprintf(file, "%d年%d月%d日,星期%d,%d:%d:%d", 
		timerow->tm_year + 1900,
		timerow->tm_mon + 1,
		timerow->tm_mday, 
		temp,
		timerow->tm_hour,
		timerow->tm_min,
		timerow->tm_sec);

        // 延时一秒
        sleep(1);

        // 将光标重新指向文件头
        fseek(file, 0, SEEK_SET);
    }
}
posted @ 2024-05-08 22:06  Dazz_24  阅读(9)  评论(0编辑  收藏  举报