又开始写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”;