Like Tree1Likes
  • 1 Post By guitargod

bindery file read error

This is a discussion on bindery file read error within the C Programming forums, part of the General Programming Boards category; I wrote the following code to read bmp file and write it to new bmp file that is, to create ...

  1. #1
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56

    bindery file read error

    I wrote the following code to read bmp file and write it to new bmp file
    that is, to create a duplicate the bmp file.

    but the created bmp file is not in correct format

    when opened shows an error "this is not valid bmp file"
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    struct BITMAPFILEHEADER{
       unsigned short type;
       unsigned long size;
       unsigned short reserved1;
       unsigned short reserved2;
       unsigned long offsetbits;
    };
    
    
    
    struct BITMAPINFOHEADER{
       unsigned long size;
       unsigned long width;
       unsigned long height;
       unsigned short planes;
       unsigned short bitcount;
       unsigned long compression;
       unsigned long sizeimage;
       long xpelspermeter;
       long ypelspermeter;
       unsigned long colorsused;
       unsigned long colorsimportant;
    };
    
    
    
    struct SINGLE_PIXEL{
       unsigned char blue;
       unsigned char green;
       unsigned char red;
    };
    
    int i=0;
    int S=0;
    struct BITMAPFILEHEADER source_head;
    struct BITMAPINFOHEADER source_info;
    struct SINGLE_PIXEL source_pix;
    
    struct head dist_head;
    struct BITMAPINFOHEADER dist_info;
    struct SINGLE_PIXEL dist_pix;
    
    
    FILE *fp;
    FILE *Dfp;
    
    fp=fopen("rajni.bmp","rb");
    Dfp=fopen("rajni2.bmp","wb");
    
    fread(&source_head,sizeof(struct head),1,fp);
    fread(&source_info,sizeof(struct BITMAPINFOHEADER),1,fp);
    
    fwrite(&source_head,sizeof(struct head),1,Dfp);
    fwrite(&source_info,sizeof(struct BITMAPINFOHEADER),1,Dfp);
    
    S=source_info.width*source_info.height;
    
    for(i=1;i<=S;i++)
    {
    fread(&source_pix,sizeof(struct SINGLE_PIXEL),1,fp);
    fwrite(&source_pix,sizeof(struct SINGLE_PIXEL),1,Dfp);
    }
    fclose(fp);
    fclose(Dfp);
    return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    BMP file format - Wikipedia, the free encyclopedia
    There are several places in the format where you need to be careful about padding and alignment.

    First, check the sizes of your struct match the specification of the file format.

    Next, check the endian of your machine matches the specification of the file format.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    0.o
    Why don't you just declare a BYTE * and copy all of it..?
    Offcourse there may be some file architecture problems! If you want it solved, I'll work on it.
    sagar474 likes this.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    16
    What am I missing here? I don't see why you can't just exactly copy all of the file byte for byte.

  5. #5
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Quote Originally Posted by guitargod View Post
    0.o
    Why don't you just declare a BYTE * and copy all of it..?
    Offcourse there may be some file architecture problems! If you want it solved, I'll work on it.
    i need to process the image
    that is need to do some calculation on the pixels of the image. and modify the image.
    before doing this i have don this step.

  6. #6
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    I figured out the problem
    this is only a data type misuse.
    I used int for declaring i and S which. S stores the number of pixels to read or write. and i increments till it reaches to s.
    the size of s depends on height and width of the image. my image is 400x300 hence the space that is allocated previously is insufficient.
    and i changed it to unsigned long int. my stupidity is I used a signed integer where it is no question about sign while representing number of pixels.

    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file error
    By xniinja in forum C Programming
    Replies: 5
    Last Post: 06-17-2010, 03:10 PM
  2. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  3. read from file, runtime error
    By quizkiwi in forum C++ Programming
    Replies: 7
    Last Post: 08-05-2005, 08:51 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21