c语言中写入日期和时间

c语言中写入日期和时间。

1、

#include <stdio.h>
#include <time.h>

int main(void)
{
    FILE *fp;
    
    time_t current = time(NULL);
    struct tm *timer = localtime(&current);
    
    if((fp = fopen("xxx.txt","w")) == NULL)
        printf("\afile does not exist.\n");
    else
    {
        fprintf(fp, "%d - %d - %d - %d - %d - %d\n", timer -> tm_year + 1900,timer -> tm_mon + 1 ,timer -> tm_mday,
        timer -> tm_hour,timer -> tm_min,timer ->tm_sec);    
        printf("written successfully.\n");
    }
    return 0;
} 

 

 

 

posted @ 2021-06-08 12:39  小鲨鱼2018  阅读(1000)  评论(0编辑  收藏  举报