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

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.\n");
    else
    {
        fwrite(a, sizeof(double), 10, fp);
        fclose(fp); 
    }
    
    if((fp = fopen("tmp.bin", "rb")) == NULL)
        printf("\aFile open Failed.\n");
    else
    {
        fread(b, sizeof(double), 10, fp);
        fclose(fp);
        for(i = 0; i < 10; i++)
        {
            printf("b[%d] = %.2f.\n", 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.\n");
    else
    {
        fread(x, sizeof(int), 6, fp);
        printf("The last time the program ran is: %d-%d-%d; %d-%d-%d\n", 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.\n");
    else
    {
        fwrite(tmp, sizeof(int), 6, fp);
        fclose(fp);
    }
}

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

 

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

相关推荐