使用文件IO完成对图像的读写操作

int main(int argc, const char *argv[])
{
    int fd = -1;
    if((fd = open("./GGbond.bmp", O_RDWR|O_CREAT|O_APPEND, 0664)) == -1)
    {
        perror("open error");
        return -1;
    }

    //读取该图片的大小,需要将光标向后偏移2个字节
    lseek(fd, 2, SEEK_SET);
    int bmpsize = 0;
    //读取出图片的大小
    bmpsize = lseek(fd, 0, SEEK_END);
    printf("bmpsize = %d\n", bmpsize);

    char buf[bmpsize];
    //前54字节是文件头和信息头所占的空间
    //想要对图像内容进行读写操作
    //需要将光标向后偏移
    lseek(fd, 54, SEEK_SET);

    int res;

    unsigned char color[3] = {0, 255, 0};
    for(int i=0; i<200; i++)
    {
        for(int j=0; j<900; j++)
        {
            write(fd, color, sizeof(color));
            
        }
    }

    close(fd);

    return 0;
}

运行现象:

#include <myhead.h>
int main(int argc, const char *argv[])
{
    //定义一个目录
    DIR *dp = NULL;
    //打开目录
    if((dp = opendir("./")) == NULL)
    {
        perror("opendir error");
        return -1;
    }

    //定义一个文件指针,用于存储读取的文件信息
    struct dirent *rp = NULL;
    //定义时间结构体
    time_t sys_time = 0;
    sys_time = time(NULL);
    struct tm *tp; 
    //定义一个状态结构体
    struct stat st;
    //循环读取当前目录下的所有文件
    printf("文件权限    硬链接     用户ID       组ID     文件大小  月份   日期      时间   文件名\n");
    while((rp = readdir(dp)) != NULL)
    {
        tp= localtime(&sys_time); 
        //将当前文集你的信息打印出来
        printf("%#o\t\t%d\t%d\t%ld\t%d\t%d\t%d\t %02d:%02d\t%s\n",st.st_mode&0777, st.st_nlink, st.st_uid, st.st_gid, st.st_size,\
            tp->tm_mon+1,tp->tm_mday, tp->tm_hour, tp->tm_min, rp->d_name);
    }

    //关闭目录
    closedir(dp);
    return 0;
}
运行现象

思维导图:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值