C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem in reding file

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Harry
Guest





PostPosted: Mon May 21, 2007 9:11 am    Post subject: Problem in reding file Reply with quote



#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/
Cool);
if(top==NULL)
printf("Error allocating memory\n");


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




fread(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
Cool),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/Cool),
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/
Cool),fout);
fwrite(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
Cool),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?
Back to top
Jack Klein
Guest





PostPosted: Mon May 21, 2007 9:11 am    Post subject: Re: Problem in reding file Reply with quote



On 20 May 2007 23:08:29 -0700, Harry <gehariprasath (AT) gmail (DOT) com> wrote
in comp.lang.c:

Quote:
#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 <stdlib.h>.

Quote:
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)

Quote:
{

char ch;

ch should be defined as an int, the way you use it.

Quote:
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.

Quote:
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.

Quote:
fout=fopen("scramble.bmp","w");

Repeat the phrase above another 100 times.

Quote:
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.

Quote:
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;
Quote:

}

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?

Quote:
Cool);
if(top==NULL)
printf("Error allocating memory\n");


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




fread(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
Cool),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/Cool),
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/
Cool),fout);
fwrite(top,1,((info->height/2)*(info->width)*(info->bits_pixel/
Cool),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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.