c语言 13-12

Posted 小虾米2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言 13-12相关的知识,希望对你有一定的参考价值。

1、

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

char data_file[] = "datatime.dat";

void get_data(void)
{
    FILE *fp;
    
    if((fp = fopen(data_file, "r")) == NULL)
        printf("The grogram is running for the first time.\\n");
    else
    {
        int tmp[6];
        fread(tmp, sizeof(int), 6, fp);
        printf("The frogram ran for the last time: %d-%d-%d; %d-%d-%d\\n", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]);
        fclose(fp);
        
        /*int year, month, day, hour, min, sec;
        fscanf(fp, "%d%d%d%d%d%d", &year, &month, &day, &hour, &min, &sec);
        printf("The frogram ran for the last time: %d-%d-%d; %d-%d-%d\\n", year, month, day, hour, min, sec);
        fclose(fp);*/
    }    
} 

void put_data(void)
{
    FILE *fp;
    time_t current = time(NULL);
    struct tm *timer = localtime(&current);
    
    if((fp = fopen(data_file, "w")) == NULL)
        printf("\\aFile open failed.\\n");
    else
    {
        int tmp[6] = {timer -> tm_year + 1900, timer -> tm_mon + 1, timer -> tm_mday, 
        timer -> tm_hour, timer -> tm_min, timer -> tm_sec};
        fwrite(tmp, sizeof(int), 6, fp);
        
        /*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);*/
        fclose(fp);
    }
}

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

 

以上是关于c语言 13-12的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 有用的代码片段和 trick

我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段

我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段

SQL Select 语句的用法

C语言100个经典算法源码片段

需要示例代码片段帮助