I want to read file in binary mode using fread() into structure members.
following is the code which works perfectly on Turbo C compiler(DOS base) but doesn't work for VC++ 6, it is giving exception at fread() statement.
The input to this file we can give any .bmp file name e.g. abc.bmp
output
header->bfType 19778
.
.
header->bfOffBits 54
//Program Starts here
#include<stdio.h>
#include<conio.h>
typedef unsigned int WORD;
typedef unsigned long DWORD;
typedef unsigned char BYTE;
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
char f1[20];
FILE *in;
BITMAPFILEHEADER *header;
void main()
{
printf("\nEnter input file name: ");
scanf("%s",f1);
//printf("\nEnter output file name: ");
//scanf("%s",f2);
/*OPEN INPUT IMAGE FILE(.BMP)*/
if ((in=fopen(f1, "rb")) == NULL)
{
fprintf(stderr, "Cannot open input file.\n");
return 1;
}
else
{
fread(&header->bfType, sizeof(header->bfType), 1, in);
fread(&header->bfSize, sizeof(header->bfSize), 1, in);
fread(&header->bfReserved1, sizeof(header->bfReserved1), 1, in);
fread(&header->bfReserved2, sizeof(header->bfReserved2), 1, in);
fread(&header->bfOffBits, sizeof(header->bfOffBits), 1, in);
printf("\nheader->bfType %u",header->bfType);
printf("\nheader->bfSize %ld",header->bfSize);
printf("\nheader->bfReserved1 %u",header->bfReserved1);
printf("\nheader->bfReserved2 %u",header->bfReserved2);
printf("\nheader->bfOffBits %ld",header->bfOffBits);
}
getch();
}