Hi! I was trying to read, manipulate and save some BMP files. My program simlpy reads a bmp file then prints the size of the file (present inside the bmp file itself). The problem is that the size remains zero. I have used xxd to see the first bytes of the file and the size is there, and it is different from zero.

Here is the code of bitmap.h:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#define BMP_TYPE 19778

typedef struct{
    unsigned short type;                 /* Magic identifier            */
    unsigned int size;                       /* File size in bytes          */
    unsigned short int reserved1, reserved2;
    unsigned int offset;                     /* Offset to image data, bytes */
    unsigned int hsize;               /* Header size in bytes      */
    int width,height;                /* Width and height of image */
    unsigned short int planes;       /* Number of colour planes   */
    unsigned short int bits;         /* Bits per pixel            */
    unsigned int compression;        /* Compression type          */
    unsigned int imagesize;          /* Image size in bytes       */
    int xresolution,yresolution;     /* Pixels per meter          */
    unsigned int ncolours;           /* Number of colours         */
    unsigned int importantcolours;   /* Important colours         */
} HEADER;

typedef struct{
    HEADER header;
    char* data;
} BMP;


void loadFile( char* fileName, BMP** bmp  );
void writeFile( char* fileName, BMP* bmp  );
And here is the code of bitmap.c:

Code:
#include "bitmap.h"

void loadFile( char* filenName, BMP** pbmp  ){

    struct stat stat_file; 
    FILE* file; 
    BMP* bmp;

    // recover file statistics
    stat( filenName, &stat_file );

    *pbmp = (BMP*) malloc( stat_file.st_size );
    if( !(*pbmp) ){
	fprintf(stderr, "It was not possible to allocate some memory.\n");
	exit(1);
    }
    
    // to ease manipulation
    bmp = *pbmp;

    // open bitmap file
    file = fopen( filenName, "r" );

    // read all the file into pbmp structure
    fread( bmp, 1, stat_file.st_size, file );
    
    // we must convert from little-endian to big-endian
    // the error is here, inspection of the file 
    // shows that size is non zero
    //  printf("File size is %d\n", ntohl(bmp->header.size) );
    printf("File size is %d\n",  (((char*)bmp)[2]));
}

void writeFile( char* filenName, BMP* bmp  ){

    struct stat stat_file;

    int file = open( filenName, O_RDWR );
    if( write( file, bmp, stat_file.st_size ) < stat_file.st_size ){
	
	perror( "Error saving file.\n" );
	exit(1);
    }
}
Thanks any help!