#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;
}
【无标题】标准IO操作文件
于 2022-08-03 20:36:43 首次发布