【无标题】标准IO操作文件

#include <head.h>
typedef struct picture {
    unsigned int size;
    unsigned int offset;
    unsigned int width;
    unsigned int high;
    unsigned short pix;
} p_t;

typedef struct {
    unsigned char B; //蓝
    unsigned char G; //绿
    unsigned char R; //红
} RGB_t;

void get_picture_head_info(FILE* fp, p_t* info)
{
    fseek(fp, 2, SEEK_SET);
    fread(&info->size, 4, 1, fp);

    fseek(fp, 10, SEEK_SET);
    fread(&info->offset, 4, 1, fp);

    fseek(fp, 18, SEEK_SET);
    fread(&info->width, 4, 1, fp);
    fread(&info->high, 4, 1, fp);

    fseek(fp, 2, SEEK_CUR);
    fread(&info->pix, 2, 1, fp);
}

void show_picture_head_info(p_t* info)
{
    printf("size = %d,offset=%d,width=%d,high=%d,pix = %d\n",
        info->size, info->offset, info->width, info->high, info->pix);
}

int main(int argc, const char* argv[])
{
    FILE* fp;
    p_t info;
    RGB_t rgb;
    if (argc != 2) {
        fprintf(stderr, "input error,try again\n");
        fprintf(stderr, "usage:./a.out picturename\n");
        return -1;
    }

    if ((fp = fopen(argv[1], "r+")) == NULL)
        PRINT_ERR("fopen error");

    get_picture_head_info(fp, &info);
    show_picture_head_info(&info);

    fseek(fp, 427*info.width*info.pix/8+255*info.pix/8+info.offset, SEEK_SET);

    memset(&rgb, 0, sizeof(rgb));
    rgb.G = 255;
    rgb.B = 0;
    rgb.R = 0;
    for (int i = 0; i < 43; i++) {
        for (int j = 0; j < 66; j++) {
            fwrite(&rgb, 3, 1, fp);
        }
         fseek(fp, (info.width-66)*info.pix/8, SEEK_CUR);
    }
    fclose(fp);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值