0

I'm trying to read the header of a bmp image and get the width and height of it into my struct, but as you can see in my code, i was trying to fread file header into my char array header, it shows BM6 in terminal and i really have no idea what is it. And how to pass them into my struct.

#include <stdio.h>

struct __attribute__((__packed__)) BitmapHeader
{
    int width;
    int height;
};

void loadBmp(char* filePath, struct BitmapHeader *bmpHeaderInfo){ //pass struct by ref
    FILE* filePointer = fopen(filePath, "rb");
    if (filePointer == NULL)
        return NULL;//temp!


    unsigned char header[54];
    fread(header, sizeof(unsigned char), 54, filePtr);
    //fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
    printf("%s\n", header);

}
int main(){
    char path2BMP[] = "cup.bmp";
    struct BitmapHeader bmpHeaderInfo = {0};
    loadBmp(path2BMP, &bmpHeaderInfo);
    return 0;
}
sashoalm
  • 75,001
  • 122
  • 434
  • 781
libra
  • 673
  • 1
  • 10
  • 30
  • 1
    [See related](https://stackoverflow.com/questions/36326567/terminated-by-signal-sigsegv-when-reading-bmp-file-in-c). `&bitmapFileHeader` - um, `bitmapFileHeader` is already a pointer in `loadBmp`. lose the `&` when calling `fread`. And we can only home `BITMAPFILEHEADER` is the same size as `BitmapHeader`. And please, spend a little more time learning how the link I provided in the *last* question works. – WhozCraig Mar 31 '16 at 07:58
  • @WhozCraig, yea my bad, comment out, it was copied from other's code – libra Mar 31 '16 at 07:59
  • You shouldn't need to define those structs, they should already be defined by whatever API you are using (Windows?). – Lundin Mar 31 '16 at 08:18
  • @Lundin, ubuntu, huhnn, but what should i do if i delete my struct pls? – libra Mar 31 '16 at 08:21
  • 1
    @libra See https://stackoverflow.com/questions/17967480/parse-read-bitmap-file-in-c – sashoalm Mar 31 '16 at 08:32
  • @sashoalm thx!, that solved my problem – libra Mar 31 '16 at 08:55

0 Answers0