0

ビットマップ画像を開いて寸法を画像に保存するプログラムを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[count])=='B' && fgetc(fp[count])=='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

2つの問題。

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

する必要があります

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

あなたは間違った量を「スキップ」しています

// read 1 integer (likely size 4)
fread(&bmp[temp1].width, sizeof(int), 1, fp);
// Skip 2 bytes
fskip(fp,2);
// read another integer (likely size 4)
fread(&bmp[temp1].height,sizeof(int), 1, fp);

1 つのソリューション

fread(&bmp[temp1].width, sizeof(int), 1, fp);
// Don't skip - you are in the right location.
fread(&bmp[temp1].height,sizeof(int), 1, fp);

より良い解決策

typedef struct tagBITMAP              /* The structure for a bitmap. */
{
   uint32_t width;
   uint32_t height;
} BITMAP;

fread(&bmp[temp1].width, sizeof(bmp[temp1].width) , 1, fp);
fread(&bmp[temp1].height,sizeof(bmp[temp1].height), 1, fp);
于 2013-07-01T19:17:57.173 に答える