473,785 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_he ader;
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("scr amble.bmp","w") ;
if(fp==NULL)
printf("Error opening file\n");
fread(&info->sig,2,1,fp);
fread(&info->size_bmp,4,1,f p);
fread(&info->res1,2,1,fp) ;
fread(&info->res2,2,1,fp) ;
fread(&info->offset,4,1,fp) ;
fread(&info->size_bmpinfo_h eader,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("Signatu re-----------%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_h eader);
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(c h)=='y');
scramble();

fclose(fp);

}

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

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


fread(top,1,((i nfo->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,f out);
fwrite(&info->res1,1,2,fout) ;
fwrite(&info->res2,1,2,fout) ;
fwrite(&info->offset,1,4,fou t);
fwrite(&info->size_bmpinfo_h eader,1,4,fout) ;
fwrite(&info->width,1,4,fout );
fwrite(&info->height,1,4,fou t);
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,fo ut);
fwrite(&info->ver_res,1,4,fo ut);
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 2079
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_he ader;
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("scr amble.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,f p);
fread(&info->res1,2,1,fp) ;
fread(&info->res2,2,1,fp) ;
fread(&info->offset,4,1,fp) ;
fread(&info->size_bmpinfo_h eader,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("Signatu re-----------%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_h eader);
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(c h)=='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*)mall oc((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*)m alloc((info->height/2)*(info->width)*(info->bits_pixel/
8));
if(bottom==NULL )
printf("Error allocating memory\n");


fread(top,1,((i nfo->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,f out);
fwrite(&info->res1,1,2,fout) ;
fwrite(&info->res2,1,2,fout) ;
fwrite(&info->offset,1,4,fou t);
fwrite(&info->size_bmpinfo_h eader,1,4,fout) ;
fwrite(&info->width,1,4,fout );
fwrite(&info->height,1,4,fou t);
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,fo ut);
fwrite(&info->ver_res,1,4,fo ut);
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.l earn.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,((i nfo->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*******@spam cop.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
3782
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 days and would appreciate this group's helpful expertise. My problem may be related to mixing the C and C++ languages together. Namely, I have a struct which I cannot change (as legacy code) similiar to this (I will change the names throughout as...
6
5623
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: " The document was understood, but it could not be processed. - The WSDL document contains links that could not be resolved. - There was an error downloading
12
8502
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. I, too, am getting nasty FileNotFound exceptions. I've read, and digested the article, and I think I've found a bug -- it's difficult to track, though it does happen often.
8
3006
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 RH, the call of sqlj.install_jar fails. It correctly create a directiory with the correct schema name, but the jar is not copied in it. The error is "Permission Denied" SQLSTATE 38501 and in the log, we can see that calling sqlejReadJar fails in...
11
6421
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 requiring for me to alter some existing code to a program. While running the unaltered code, I kept encountering an application (The exception unknown software exception(0xc00000fd) etc. .. etc... etc... I am running W2K Pro). Through a bit of...
11
6639
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 transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
0
1824
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 ---------------------------------------------------- System.Data.OleDb.OleDbException: No error information available: E_NOINTERFACE(0x80004002). ---------------------------------------------------- I have the schema.ini file in the folder with the delimited text file
8
9764
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 problem: my vb.net program has problems with UNC. If the UNC server is restarted or goes off-line, my VB.net program crashes. The code for UNC access to the file is included below and is put in the tick event of a form timer control running every...
4
4067
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 written A2003 and they use the A2003 runtime to access the application. They use a desktop icon that specifies the location of Access, the Appname, and the MDW file to use. Ex: "C:\ProgramFiles\Office\MSACCESS.EXE" "C:\Apps\App.MDB" /wrkgrp...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.