微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c语言中获取程序上一次运行时的信息

1、

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

char file[] = "datatime.dat";

void put_data(void)
{
    FILE *fp;
    
    if((fp = fopen(file,"r")) == NULL)  // 未检测到datatime.dat文件显示第一次运行 
        printf("\athe program is run for the first time.");
    else  // 若是检测到datatime.dat文件,读取文件内容并显现 
    {
        int year, month, day, h, m, s;
        fscanf(fp, "%d%d%d%d%d%d", &year, &month, &day, &h, &m, &s);
        printf("last run time is: %d-%d-%d-%d-%d-%d\n", year, month, day, h, m, s);
        fclose(fp);
    }
}

void get_data(void)
{
    FILE *fp;
    
    time_t current = time(NULL);
    struct tm *timer = localtime(&current);
    
    if((fp = fopen(file, "w")) == NULL)
        printf("\afile open Failed.\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);   // 将程序运行的时间写入到文件datatime.dat中 
        fclose(fp);
    }
}

int main(void)
{
    put_data();
    get_data();
    
    return 0;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐