c语言 13-11、13-12

1、13-11

#include <stdio.h>

int main(void)
{
    FILE *fp;
    int i;
    double b[10];
    double a[] = {0.1,1.1,2.1,3.1,4.1,5.1,6.1,7.1,8.1,9.1};
    
    if((fp = fopen("tmp.bin", "wb")) == NULL)
        printf("aFile open failed.
");
    else
    {
        fwrite(a, sizeof(double), 10, fp);
        fclose(fp); 
    }
    
    if((fp = fopen("tmp.bin", "rb")) == NULL)
        printf("aFile open failed.
");
    else
    {
        fread(b, sizeof(double), 10, fp);
        fclose(fp);
        for(i = 0; i < 10; i++)
        {
            printf("b[%d] = %.2f.
", i, b[i]);
        }
    }
    return 0;
}

2、13-12

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

void get_data(void)
{
    FILE *fp;
    int x[6];
    
    if((fp = fopen("time.bin", "rb")) == NULL)
        printf("aThe program is running for the first time.
");
    else
    {
        fread(x, sizeof(int), 6, fp);
        printf("The last time the program ran is: %d-%d-%d; %d-%d-%d
", x[0], x[1], x[2], x[3],x[4],x[5]);
        fclose(fp);
    }
}

void put_data(void)
{
    FILE *fp;
    time_t current = time(NULL);
    struct tm *timer = localtime(&current);
    
    int tmp[6] = {timer -> tm_year + 1900, timer -> tm_mon + 1, timer -> tm_mday, timer -> tm_hour, timer -> tm_min, timer -> tm_sec};
    
    if((fp = fopen("time.bin", "wb")) == NULL)
        printf("aFile open failed.
");
    else
    {
        fwrite(tmp, sizeof(int), 6, fp);
        fclose(fp);
    }
}

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

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14874549.html