7.1作业

思维导图

1将当前的时间写入到time. txt的文件中,如果ctrl+c退出之后,在再次执行支持断点续写
     1.2022-04-26 19:10:20
     2.2022-04-26 19:10:21
     3.2022-04-26 19:10:22
     //按下ctrl+c停止,再次执行程序
     4.2022-04-26 20:00:00
     5.2022-04-26 20:00:01

#include "head.h"
int main() 
{   
    int T = 0;
    int num = 1;  // 计数变量
    FILE *fp = fopen("time.txt", "a+");
    if (fp == NULL) 
    {
        perror("fopen error");
        return 1;
    } 
    
    // 读取文件确定起始编号
    char line[256];
    
    rewind(fp);
    
    int last_num = 0;
    while (fgets(line, sizeof(line), fp) != NULL) 
    {
        int current_num;
        if (sscanf(line, "%d.", &current_num) == 1) 
        {
            if (current_num > last_num) {
                last_num = current_num;
            }
        }   
    }
    
    // 设置下一个编号
    num = last_num + 1;
    
    fseek(fp, 0, SEEK_END);
    
    while(1)
    {
        time_t t;
        time(&t);
        struct tm *s = localtime(&t);
        if (s == NULL) 
        {
            perror("localtime error");
            fclose(fp);  // 关闭已打开的文件
            return 1;
        }
        
        // 每秒写入一次时间
        if(t != T)
        {
            fprintf(fp, "%d.%d-%02d-%02d %02d:%02d:%02d\n", num++, 
                   s->tm_year + 1900, s->tm_mon + 1, s->tm_mday,
                   s->tm_hour, s->tm_min, s->tm_sec);
            fflush(fp);  // 确保数据写入文件
            T = t;       // 更新时间戳
        }
    }
    
    fclose(fp);  
    return 0;
}
 

2.使用文件IO函数实现图片的拷贝

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值