文件操作二之bmp

又开始写C代码了,倍感亲切啊。

使用标准I/O库函数 “fread”来读取文件,并把读取的文件的内容对应填写在事先定义好的结构体中,而本次试验所做的事情是读取文件的文件头,以及文件的特征数据。在此要事先找到对于bmp文件的文件头定义格式(很奇怪为什么大家都从bmp开始,这倒霉孩子招谁惹谁了)。定义一个关于BMPFILEHEADER的结构体。对于文件的特征数据,则定义一个BMPFILEINFOHEADER,额有点长,但是把英文翻译过来还是很好记的,所以,加紧学习英语吧。

#include <stdio.h>   //包含了fread函数等

#include <string.h>

#include <sys/type.h>

 

//开始先定义关于BMP特征的头文件以及特征文件

typedef struct BITFILEHEADER

{

    u_int16_t  bfType;

    u_int16_t bfSize;

    u_int16_t bfReserved1;

    u_int16_t bfReserved2;

    u_int32_t biOffBits;

}  BITFILEHEADER;

typedef struct BITFILEINFOHEADER

{

    u_int32_t biSize;

    u_int32_t biWidth;

    u_int32_t biHeight;

    u_int16_t biPlanes;

    u_int16_t biBitCount;

    u_int32_t biCompression;

    u_int32_t biSizeImage;

    u_int32_t biXPelsPerMeter;

    u_int32_t biYPelsPerMeter;

    u_int32_t biClrUsed;

    u_int32_t biClrImportant;

}     BITFILEINFOHEADER;

 

int main ( )

{

    FILE *fp = fopen ("/home/Upan/4.4.bmp", "rb");

   

    BITFILEHEADER myhead;

    BITFILEINFOHEADER myinfo;

  

    fread (&myhead, 1, sizeof (BITFILEHEADER) ,fp );

    fread (&myinfo , 1,sizeof (BITFILEINFOHEADER) , fp);

   

    printf ("The size of BITFILEHEADER is %d \n " , sizeof (BITFILEHEADER));

    printf ("The type number of the file is %d \n " ,myhead.bfType);

 

     fclose (fp);

     return 0;

}

 

 

终于弄完了这个程序,改程序执行之后返回

The size of BITFILEHEADER is 14

The type number of the file is 19778

 

 

恩,写了几天shell有点不习惯C了。

在函数的返回值里面文件类型码 19778 对应于“BM”;

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值