Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading bmp file

Status
Not open for further replies.
Oct 29, 2004
6
AU
hi guys...here i am trying to read bmp file & when i run this code in turbo c ..its wokring properly when it comes to vc++ compiler i got winxp assertion window.. i think i did somthing wrong with pointers..can u help me out plzzz
#include<stdio.h>
#include<conio.h>
typedef unsigned long lword;
typedef unsigned short word;
typedef unsigned char byte;
typedef struct bmpheader
{
char type[2];
lword size;
lword reser;
lword offset;
lword headersize;
lword width;
lword height;
lword planes;
}bmp_header;
int main()
{
int i,x,y;
int result;
char *filename;
bmp_header *bmp_ptr;
clrscr();
printf("enter file name");
scanf("%s",filename);
result=bmp_load(filename,bmp_ptr);
printf("\nvalid_0 invalid_1 bmpfile %d ",result);


}
int bmp_load(char *filename,bmp_header *bmp_ptr)
{
FILE *fp;
bmp_ptr->type[0]=' ';
if((fp=fopen(filename,"rb"))==NULL)
{
printf("cant open file");
return(1);
}
fread(&bmp_ptr->type,2,1,fp);

if((bmp_ptr->type[0]!='B')||(bmp_ptr->type[1]!='M'))
return(1);
fread(&bmp_ptr->size,52,1,fp);

if(bmp_ptr->headersize!=40)
return(1);

printf("\ndata\t %lu",bmp_ptr->width);


fclose(fp);
getche();

}

 
Yes, with the exception of fp, every single pointer use in your code is incorrect.

DOS is like that, no memory protection whatsoever. Which means (for small programs at least), all kinds of pointer abuse are tolerated so that people say "it works for me".

>char *filename;
>bmp_header *bmp_ptr;
Should be
Code:
char filename[100];
bmp_header bmp;

The scanf call is OK (in as much as scanf can ever be OK, but that's another story). The call to your function should be.
Code:
result=bmp_load(filename,&bmp);
It is NOT sufficient to simply go round declaring variables of the correct type. When you're dealing with pointers, you HAVE to make sure they're pointing somewhere.

> fread(&bmp_ptr->size,52,1,fp);
Do NOT assume that the size of a structure is equal to the sum of the sizes of its members. To preserve data alignment, compilers add padding bytes.
It also means that trying to map a published file format directly onto a structure will be more difficult than it first seems.

In your case, be prepared for the headersize member being in the wrong place with respect to the position of that value in the file.

--
 
thanks for u r help salem,
but as u said ,do u want to me to remove all the pointers??? if i use filename[100] iinstead of *filename, dont u think we r wasting lots of memory and any more suggestion for fread.
thank you
 
> but as u said ,do u want to me to remove all the pointers???
Yes.

> if i use filename[100] iinstead of *filename dont u think we r wasting lots of memory
Well your way isn't using any memory at all - well none that you know about. C does not dynamically allocate and free just what it needs when you do something like
Code:
char *p;
scanf("%s",p);
There is no magic to allocate just enough space for whatever you type, then some similar magic later on to get rid of that space. Whatever random address p is pointing at, that is where your input will go. If that trashes something critical, well then that's your bad luck.

> and any more suggestion for fread.
Lets work on understanding pointers first...

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top