【IO】 对于fread,fwrite,time_t,fseek等操作,写日志文件,完成文件的拷贝,对bmp图像进行读写操作;

目录

1>写一个日志文件,将程序启动后,每一秒的时间写入到文件中

​编辑

2>使用fread、fwrite完成两个文件的拷贝

3>实现对bmp图像的读写操作


1>写一个日志文件,将程序启动后,每一秒的时间写入到文件中

1、2024-7-29 10:31:19
2、2024-7-29 10:31:20
3、2024-7-29 10:31:21
:ctrl+c:停止程序
/a.out
4、2024-7-29 10:35:06
5、2024-7-29 10:35:07
6、2024-7-29 10:35:08

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


int read_count();
void write_count(int count);

int main(int argc, char const *argv[])
{
    // 定义变量存储秒数
    time_t new_time;
    time_t last_time = 0;

    // 存放转变后的字符串
    char buf[128] = "";

    // 序号变量,从文件读取
    int count = read_count();

    while(1)
    {
        new_time = time(NULL);

        if(new_time != last_time)
        {
            // 将秒数转换为结构体
            struct tm *time_ptr = localtime(&new_time);

            // 把当前时间转换成字符串放入字符数组中,并添加序号
            sprintf(buf, "%d. %4d-%02d-%02d %02d:%02d:%02d\n",
                    count,
                    time_ptr->tm_year + 1900,
                    time_ptr->tm_mon + 1,
                    time_ptr->tm_mday,
                    time_ptr->tm_hour,
                    time_ptr->tm_min,
                    time_ptr->tm_sec);

            // 打印当前时间
            printf("%s", buf);

            last_time = new_time;
            count++; // 更新序号

            // 将更新后的序号写入文件
            write_count(count);
        }
    }

    return 0;
}

// 从文件读取计数器的值
int read_count() {
    FILE *fp = fopen("count.txt", "r");
    if (fp == NULL) {
        // 如果文件不存在,返回1开始计数
        return 1;
    }
    int count;
    if (fscanf(fp, "%d", &count) != 1) {
        // 如果读取失败,返回1开始计数
        count = 1;
    }
    fclose(fp);
    return count;
}

// 将计数器的值写入文件
void write_count(int count) {
    FILE *fp = fopen("count.txt", "w");
    if (fp != NULL) {
        fprintf(fp, "%d", count);
        fclose(fp);
    }
}

输出结果如下:


2>使用fread、fwrite完成两个文件的拷贝

#include <myhead.h>

int main(int argc, char const *argv[])
{
    FILE *fp = NULL, *fpw = NULL;
    char temp;

    //判断输入是否符合要求
    if(argc != 2)
    {
        printf("You should input:./a.out file_name\n");
        return -1;
    }

    // 打开源文件用于读取
    if ((fp = fopen(argv[1], "r")) == NULL) 
    {
        perror("fopen error");
        return -1;
    }

    // 创建或打开目标文件用于写入
    if ((fpw = fopen("./user_copy.txt", "w")) == NULL) 
    {
        perror("fopen error");
        fclose(fp);
        return -1;
    }

    // 读取源文件并写入目标文件
    while (fread(&temp, sizeof(temp), 1, fp) == 1)
    {
        fwrite(&temp, sizeof(temp), 1, fpw); // 写入目标文件流 fpw
    }

    //关闭文件
    fclose(fp);
    fclose(fpw);
    return 0;
}

输出结果如下:

可以看到已经把02fcopy.c文件拷贝到了目标文件user_copy.txt里


3>实现对bmp图像的读写操作

#include<myhead.h>

int main(int argc, char const *argv[])
{
    FILE *fp = NULL;
    if((fp = fopen("./gg.bmp","r+")) == NULL)
    {
        perror("fopen error");
        return -1;
    }

    //获取文件大小
    int img_size = 0;

    //将文件光标从头开始偏移2个字节
    fseek(fp,2,SEEK_SET);

    //读取4字节的内容,这就是图像的大小
    fread(&img_size,sizeof(img_size),1,fp);
    printf("size = %d\n",img_size);

    //从头向后偏移54个字节,就是图像数据
    fseek(fp,54,SEEK_SET);

    //定义一个像素
    unsigned char color[3] = {255,0,0};  //蓝色

    for(int i = 0;i < 960;i++)
    {
        for(int j = 0;j < 1280;j++)
        {
            fwrite(color,sizeof(color),1,fp);
        }
    }

    fclose(fp);

    return 0;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值