473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem in reding file

#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?

May 21 '07 #1
3 2051
On 20 May 2007 23:08:29 -0700, Harry <ge***********@gmail.comwrote
in comp.lang.c:
#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>.
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
May 21 '07 #2
Harry wrote:
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
May 21 '07 #3
On Mon, 21 May 2007 02:02:25 -0500, Jack Klein <ja*******@spamcop.net>
wrote:

[snipped lots of very pertinent remarks]
> fread(&info->sig,2,1,fp);
Incorrect. BMP files contain binary numbers in little-endian format,
which is not necessarily the same as native-endian.
> fwrite(&info->sig,1,2,fout);
Ditto.

May 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Keith Dewell | last post by:
Greetings! My current job has brought me back to working in C++ which I haven't used since school days. The solution to my problem may be trivial but I have struggled with it for the last two...
6
by: trexim | last post by:
Hi, I am trying to create a Web Reference for CSTA using the URL http://www.ecma-international.org/standards/ecma-348/csta-wsdl/csta-wsdl-all-operations.wsdl Visual .Net complains that: "...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
8
by: Xela | last post by:
Hi A have a very annoying problem. I have written java strored procedures for DB2 v8.1. Their deployement and usage is fine as long as the server is a Windows one. But under Solaris 8 and Linux...
11
by: Marcus Jacobs | last post by:
Dear Group I have encountered a problem with fclose and I am wondering if anyone could provide some insight about this problem to me. Currently, I am working on a small personal project that is...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
0
by: TJS | last post by:
attempting to read a delimited text file into a dataset using oledb text file connection getting this error message when trying to open connection...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
4
by: Salad | last post by:
I have a situation where some, not all, users get the message "Couldn't find file "F:\AccessApps\AppName.mdw". This file is required for startup". My app the users are attempting to access is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.