Problem in reding file

H

Harry

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

void scramble(void);

struct bmp_header
{
short int sig;
int size_bmp;
short int res1;
short int res2;
int offset;
int size_bmpinfo_header;
int width;
int height;
short int no_planes;
short int bits_pixel;
int comp_type;
int size_imgdata;
int hor_res;
int ver_res;
int no_colors;
int no_impcolors;

};

FILE *fp;
FILE *fout;
struct bmp_header *info;

main()
{

char ch;
info=malloc(54);
fp=fopen("duck.bmp","r");

fout=fopen("scramble.bmp","w");
if(fp==NULL)
printf("Error opening file\n");


fread(&info->sig,2,1,fp);
fread(&info->size_bmp,4,1,fp);
fread(&info->res1,2,1,fp);
fread(&info->res2,2,1,fp);
fread(&info->offset,4,1,fp);
fread(&info->size_bmpinfo_header,4,1,fp);
fread(&info->width,4,1,fp);
fread(&info->height,4,1,fp);
fread(&info->no_planes,2,1,fp);
fread(&info->bits_pixel,2,1,fp);
fread(&info->comp_type,4,1,fp);
fread(&info->size_imgdata,4,1,fp);
fread(&info->hor_res,4,1,fp);
fread(&info->ver_res,4,1,fp);
fread(&info->no_colors,4,1,fp);
fread(&info->no_impcolors,4,1,fp);

printf("Signature-----------%X\n",info->sig);
printf("Size of Bmp File----%d\n",info->size_bmp);
printf("Offset to Image data-%d\n",info->offset);
printf("Size of Header-------%d\n",info->size_bmpinfo_header);
printf("width----------------%d\n",info->width);
printf("Height---------------%d\n",info->height);
printf("Bits per pixel-------%d\n",info->bits_pixel);
printf("size of image data---%d\n",info->size_imgdata);



printf("Do you want to scramble image:(y/n)\n");
while(getchar(ch)=='y');
scramble();

fclose(fp);

}

void scramble()
{
char *top,*bottom;

top=(char*)malloc((info->height/2)*(info->width)*(info->bits_pixel/
8));
if(top==NULL)
printf("Error allocating memory\n");


bottom=(char*)malloc((info->height/2)*(info->width)*(info->bits_pixel/
8));
if(bottom==NULL)
printf("Error allocating memory\n");




fread(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fp);

if(ferror(fp)!=0)
printf("Error reading stream\n");


if(feof(fp)!=0)
printf("End of file reached\n");


fread(bottom,((info->height/2)*(info->width)*(info->bits_pixel/8)),
1,fp);
fwrite(&info->sig,1,2,fout);
fwrite(&info->size_bmp,1,4,fout);
fwrite(&info->res1,1,2,fout);
fwrite(&info->res2,1,2,fout);
fwrite(&info->offset,1,4,fout);
fwrite(&info->size_bmpinfo_header,1,4,fout);
fwrite(&info->width,1,4,fout);
fwrite(&info->height,1,4,fout);
fwrite(&info->no_planes,1,2,fout);
fwrite(&info->bits_pixel,1,2,fout);
fwrite(&info->comp_type,1,4,fout);
fwrite(&info->size_imgdata,1,4,fout);
fwrite(&info->hor_res,1,4,fout);
fwrite(&info->ver_res,1,4,fout);
fwrite(&info->no_colors,1,4,fout);
fwrite(&info->no_impcolors,1,4,fout);
fwrite(bottom,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fout);
fwrite(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fout);
fclose(fout);

}


Hi all,in this code i am trying to open a bmp file and display it's
header information which is first 54 bytes which i have succesfully
done.In the same code,I am trying to invert the image .i.e I have to
make the bottom half of the image to appear in the top half and vice-
versa.

But my file pointer reaches the end of file when i am in the function
scramble().

Why does this happen and how to eliminate it?

2)Is it possible to find the size of integer without using the
sizeof() operator?
 
J

Jack Klein

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

There is no header named "malloc.h" in the standard C library. The
prototypes for the memory allocation functions are in said:
void scramble(void);

struct bmp_header
{
short int sig;
int size_bmp;
short int res1;
short int res2;
int offset;
int size_bmpinfo_header;
int width;
int height;
short int no_planes;
short int bits_pixel;
int comp_type;
int size_imgdata;
int hor_res;
int ver_res;
int no_colors;
int no_impcolors;

};

FILE *fp;
FILE *fout;
struct bmp_header *info;

main()

The latest versions of the C standard have removed implicit int. Make
the above:

int main()

or:

int main(void)
{

char ch;

ch should be defined as an int, the way you use it.
info=malloc(54);

You seem to assume that your bmp_header struct will be exactly 54
bytes in size. There is no such guarantee in C. Also you don't check
to see if malloc() failed.
fp=fopen("duck.bmp","r");

First, you don't check to see if fopen() failed. Secondly, repeat
after me 100 times:

OPEN BINARY FILES IN BINARY MODE.
OPEN BINARY FILES IN BINARY MODE.
OPEN BINARY FILES IN BINARY MODE.
fout=fopen("scramble.bmp","w");

Repeat the phrase above another 100 times.
if(fp==NULL)
printf("Error opening file\n");


fread(&info->sig,2,1,fp);
fread(&info->size_bmp,4,1,fp);
fread(&info->res1,2,1,fp);
fread(&info->res2,2,1,fp);
fread(&info->offset,4,1,fp);
fread(&info->size_bmpinfo_header,4,1,fp);
fread(&info->width,4,1,fp);
fread(&info->height,4,1,fp);
fread(&info->no_planes,2,1,fp);
fread(&info->bits_pixel,2,1,fp);
fread(&info->comp_type,4,1,fp);
fread(&info->size_imgdata,4,1,fp);
fread(&info->hor_res,4,1,fp);
fread(&info->ver_res,4,1,fp);
fread(&info->no_colors,4,1,fp);
fread(&info->no_impcolors,4,1,fp);

printf("Signature-----------%X\n",info->sig);
printf("Size of Bmp File----%d\n",info->size_bmp);
printf("Offset to Image data-%d\n",info->offset);
printf("Size of Header-------%d\n",info->size_bmpinfo_header);
printf("width----------------%d\n",info->width);
printf("Height---------------%d\n",info->height);
printf("Bits per pixel-------%d\n",info->bits_pixel);
printf("size of image data---%d\n",info->size_imgdata);



printf("Do you want to scramble image:(y/n)\n");
while(getchar(ch)=='y');

Have you looked at your reference book, online help, or man pages for
the getchar() function? It does not take any parameters, and it
returns an int, not a char.
scramble();

fclose(fp);

You defined your main function as returning an int, with the implicit
int rule that was legal in C prior to 1999. Yet you fail to return
anything. Add:

return 0;
}

void scramble()
{
char *top,*bottom;

top=(char*)malloc((info->height/2)*(info->width)*(info->bits_pixel/

Don't cast the value returned by malloc(). You didn't do it in
main(), why do it here?
8));
if(top==NULL)
printf("Error allocating memory\n");


bottom=(char*)malloc((info->height/2)*(info->width)*(info->bits_pixel/
8));
if(bottom==NULL)
printf("Error allocating memory\n");




fread(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fp);

if(ferror(fp)!=0)
printf("Error reading stream\n");


if(feof(fp)!=0)
printf("End of file reached\n");


fread(bottom,((info->height/2)*(info->width)*(info->bits_pixel/8)),
1,fp);
fwrite(&info->sig,1,2,fout);
fwrite(&info->size_bmp,1,4,fout);
fwrite(&info->res1,1,2,fout);
fwrite(&info->res2,1,2,fout);
fwrite(&info->offset,1,4,fout);
fwrite(&info->size_bmpinfo_header,1,4,fout);
fwrite(&info->width,1,4,fout);
fwrite(&info->height,1,4,fout);
fwrite(&info->no_planes,1,2,fout);
fwrite(&info->bits_pixel,1,2,fout);
fwrite(&info->comp_type,1,4,fout);
fwrite(&info->size_imgdata,1,4,fout);
fwrite(&info->hor_res,1,4,fout);
fwrite(&info->ver_res,1,4,fout);
fwrite(&info->no_colors,1,4,fout);
fwrite(&info->no_impcolors,1,4,fout);
fwrite(bottom,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fout);
fwrite(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
8)),fout);
fclose(fout);

}


Hi all,in this code i am trying to open a bmp file and display it's
header information which is first 54 bytes which i have succesfully
done.In the same code,I am trying to invert the image .i.e I have to
make the bottom half of the image to appear in the top half and vice-
versa.

But my file pointer reaches the end of file when i am in the function
scramble().

Why does this happen and how to eliminate it?

2)Is it possible to find the size of integer without using the
sizeof() operator?

Why do you want to find the size of an integer without using the
sizeof operator? That is what it is for.

In any case, the answer is yes, you can find the size of an integer
without using sizeof. The method is left as a learning experience.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
E

Ernie Wright

Harry said:
Hi all,in this code i am trying to open a bmp file

There seems to be a recent rash of interest in doing this.
But my file pointer reaches the end of file when i am in the function
scramble().

Why does this happen and how to eliminate it?

As Jack Klein has already pointed out, you opened the file in text mode
rather than binary mode. In this mode, certain byte values in the file
are filtered or translated. Assuming you're running this code under
Windows, the byte value 0x1A (ctrl-Z) is interpreted as an end-of-file
mark. fread() won't read beyond the first occurrence of this value in
the file when the file is opened in text mode.

BMP files are binary, not text. In

fp = fopen( "duck.bmp", "rb" );

the 'b' in the mode argument causes the file to be opened in binary
mode.
info=malloc(54);

It's *very* likely that sizeof struct bmp_header is *not* 54.
(info->width)*(info->bits_pixel/8)

This is not how to calculate the number of bytes per row in a BMP. Each
row is 4-byte aligned, meaning that the size of the row in bytes must be
a multiple of 4, the smallest one large enough to hold the pixel values.

You'll also have a problem if you try to read 2-color or 16-color BMPs,
rather than just full-color ones, since for those info->bits_pixel < 8.
top=
bottom=

BMP stores images upside-down, so that the first row in the file is the
bottom row. This has no effect on whether your code works, but your use
of "top" and "bottom" will confuse human readers of the code.
fread(top,1,((info->height/2)
fread(bottom,((info->height/2)

If the height of the image is odd, you will leave behind the last row in
the file (the top row of the image). The sizes of the two halves should
be (height / 2) and (height - height / 2).
fwrite(&info->no_colors,1,4,fout);
fwrite(&info->no_impcolors,1,4,fout);
fwrite(bottom,1,

This is only right if info->no_colors == 0. If it isn't, the file
contains an indexed-color image with a color table, which occurs in the
file after the number-of-important-colors field and before the first
pixel value.

- Ernie http://home.comcast.net/~erniew
 
M

Michel Bardiaux

[snipped lots of very pertinent remarks]

Incorrect. BMP files contain binary numbers in little-endian format,
which is not necessarily the same as native-endian.

Ditto.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top