当前位置: 首页 > news >正文

IO标准函数和时间函数

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 <stdio.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>

FILE *file;  // 用于写入时间到文件

// 捕获 SIGINT 信号(Ctrl+C)处理函数
void handle_sigint(int sig) {
    if (file != NULL) {
        fclose(file);  // 关闭文件
    }
    printf("\nExiting gracefully...\n");
    exit(0);  // 退出程序
}

int main(int argc, const char *argv[]) {
    signal(SIGINT, handle_sigint);  // 捕获 SIGINT 信号,优雅退出

    // 打开 time.txt 文件进行追加写入
    file = fopen("time.txt", "a");
    if (file == NULL) {
        perror("Failed to open time.txt");
        return 1;
    }

    while (1) {
        time_t sec;
        time_t res = time(&sec);  // 获取当前时间戳

        struct tm *t = localtime(&sec);  // 转换为本地时间
        if (t == NULL) {
            perror("localtime error");
            fclose(file);  // 错误时关闭文件
            return 1;
        }

        // 格式化并输出当前时间到文件
        fprintf(file, "%d-%02d-%02d %02d:%02d:%02d\n",
                t->tm_year + 1900,   // tm_year 是从 1900 年开始
                t->tm_mon + 1,       // tm_mon 是从 0 开始
                t->tm_mday,          // tm_mday 是日期
                t->tm_hour,          // tm_hour 是小时
                t->tm_min,           // tm_min 是分钟
                t->tm_sec);          // tm_sec 是秒

        // 立即刷新文件缓冲区,以确保每次写入都能被保存
        fflush(file);

        sleep(1);  // 每秒写入一次
    }

    // 关闭文件,虽然这个部分在无限循环中不会执行
    fclose(file);
    return 0;
}

2、使用fwrite和fread函数实现图片的拷贝

prt sc ---截图

eog 文件名 ----》打开文件

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *sourceFile, *destFile;
    char *sourceFilename = "source_image.jpg";  // 源文件名
    char *destFilename = "destination_image.jpg";  // 目标文件名

    // 打开源文件,读取模式
    sourceFile = fopen(sourceFilename, "rb");
    if (sourceFile == NULL) {
        perror("Error opening source file");
        return 1;
    }

    // 打开目标文件,写入模式
    destFile = fopen(destFilename, "wb");
    if (destFile == NULL) {
        perror("Error opening destination file");
        fclose(sourceFile);
        return 1;
    }

    // 获取源文件的大小
    fseek(sourceFile, 0, SEEK_END);  // 定位到文件末尾
    long fileSize = ftell(sourceFile);  // 获取文件大小
    fseek(sourceFile, 0, SEEK_SET);  // 将文件指针移回文件开头

    // 为文件内容分配内存
    unsigned char *buffer = (unsigned char *)malloc(fileSize);
    if (buffer == NULL) {
        perror("Memory allocation error");
        fclose(sourceFile);
        fclose(destFile);
        return 1;
    }

    // 读取源文件内容
    size_t bytesRead = fread(buffer, 1, fileSize, sourceFile);
    if (bytesRead != fileSize) {
        perror("Error reading source file");
        free(buffer);
        fclose(sourceFile);
        fclose(destFile);
        return 1;
    }

    // 将内容写入目标文件
    size_t bytesWritten = fwrite(buffer, 1, bytesRead, destFile);
    if (bytesWritten != bytesRead) {
        perror("Error writing to destination file");
        free(buffer);
        fclose(sourceFile);
        fclose(destFile);
        return 1;
    }

    // 清理资源
    free(buffer);
    fclose(sourceFile);
    fclose(destFile);

    printf("Image copied successfully.\n");
    return 0;
}

相关文章:

  • excel 数据透视表的创建、快速更新、格式修改
  • 分布式锁—Redisson的可重入锁
  • 2025.3.4
  • 音频3A测试--AEC(回声消除)测试
  • python连接neo4j的方式汇总
  • 使用 Apache POI 实现 Excel 单元格合并
  • 大模型工程师学习日记(十一):FAISS 高效相似度搜索和密集向量聚类的库
  • 探究DeepSeek R1与OpenAI模型文本相似度背后的秘密
  • Ubuntu20.04双系统安装及软件安装(十二):nomachine
  • 用AI学安卓游戏开发1——控制小球上下左右移动2
  • 网络安全的八大机制
  • kaggle竞赛(初识)
  • 74道高级Java面试合集,java开发模式面试题
  • 5、使用 pgAdmin4 图形化创建和管理 PostgreSQL 数据库
  • python:pymunk + pygame 模拟六边形中小球弹跳运动
  • 使用GitLink个人建站服务部署Allure在线测试报告
  • 【Swift 算法实战】存在重复元素 III
  • 第五天 Labview数据记录(5.2 Text文件读写)
  • 鸿蒙5.0实战案例:基于webview拉起自定义键盘
  • VS2015 c++和cmake配置编程
  • wordpress破解密码/文山seo
  • 上海市建设委员会的网站查询系统/百度收录的网页数量
  • cmseasy做网站简单吗/百度下载安装2019
  • 网站开发要什么样的环境/做网站多少钱
  • 无限流量网站建设/google chrome官网
  • 设计制作商城网站/精准营销的成功案例