2

我用 C 语言编写了一个程序来打开位图图像并保存图像的尺寸。我在编写 fread() 函数时遇到了一些问题。请告诉我我编写的代码中函数的正确格式应该是什么。

在这里我使用了指针数组,因为我必须打开多个位图图像。

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>


void fskip(FILE *fp, int num_bytes)
{
   int i;
   for (i=0; i<num_bytes; i++)
      fgetc(fp);
}

typedef struct tagBITMAP              /* The structure for a bitmap. */
{
 int width;
 int height;
 //unsigned char *data;
} BITMAP;


int main()
{
    int temp1=0;
    BITMAP *bmp[50];

    FILE *fp = fopen("splash.bmp","rb");

    if (fp!=NULL && (fgetc(fp)=='B' && fgetc(fp)=='M')){

    bmp[temp1] = (BITMAP *) malloc (sizeof(BITMAP));

    fskip(fp,16);
    fread(&bmp[temp1].width, sizeof(int), 1, fp);

    fskip(fp,2);
    fread(&bmp[temp1].height,sizeof(int), 1, fp);



     fclose(fp);
     }
     else exit(0);

     getch();

     }
4

1 回答 1

0
fread(&bmp[temp1].width, sizeof(int), 1, fp);

应该:

fread(&(bmp[temp1]->width), sizeof(int), 1, fp);

因为bmp[temp1]是结构使用->运算符的地址而不是.第二个相同的错误fread()

.与名为的值变量一起使用的 DOT element selection by reference
->被称为element selection through pointer.

于 2013-07-01T18:30:43.920 回答