Thread: program crashes when copying binary file

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    45

    program crashes when copying binary file

    Hi,

    I am trying to copy a binary file (band2) using the following program:
    ------------------
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {
        FILE *fp2, *fpr2;
        fp2 = fopen("C:\\band2", "rb");
        fpr2 = fopen("C:\\r2", "wb");
        long int i, size = 1249820;
        unsigned char *b2 = malloc (sizeof(*b2));
        unsigned char *r2 = malloc (sizeof(*r2));
        for(i=0, i<size, i++){
            fscanf(fp2, "%c", &b2[i]);
        }
        for(i=0, i<size, i++){
            fprintf(fpr2, "%c", b2[i]);
        }
        free(b2); 
        free(b3); 
    }
    ---------------------
    The program crashes every time I run it. Can someone pls point out to me the mistake?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by bored_guy View Post
    Hi,

    I am trying to copy a binary file (band2) using the following program:

    The program crashes every time I run it. Can someone pls point out to me the mistake?
    I don't understand the use of *b2 as an array. It's just a pointer to an unsigned char. Same with r2. No idea why you're malloc'ing one of each of them.

    fscanf() is a function that converts data into the type you specify. Why use it for binary copying? Won't it muddle up your file's data?

    Don't you want just a large buffer, and then read into it from the file, and write from it to the copy of the file, using the binary file read and writer?

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You want to use fread() and fwrite().

    You are malloc()ing 4 bytes (the sizeof a pointer) and are trying to write 1249820 byte into that location. Yes, Virginia, that will fail.

    Since you don't need to do any character manipulation, read and write a chunk of data at a time instead of a character at a time.

    As Adak said - you only need one buffer, not two.

    Close your files when you are done with them.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    the reason im using fscanf and fprintf is cos i need to do some mathematical manipulation with each and every number in the binary file, which are in unsigned char (each number runs anywhere from 0-255). but before all those manipulations come in, i wanted to just try copying the values into new files. Actually I thought a much simpler prog would suffice for copying files:

    Code:
    FILE *fp2, *fp3, *fpr2, *fpr3;
    fp2 = fopen("C:\\band2", "rb");
    fp3 = fopen("C:\\band3", "rb");
    fpr2 = fopen("C:\\r2", "wb");
    fpr3 = fopen("C:\\r3", "wb");
    
    long int i, size = 1249820;;
    unsigned char a2[size], a3[size];
    
    for(i=0, i<size, i++){
      fscanf(fp2, "&#37;c", &a2[i];
      fscanf(fp3, "%c", &a3[i];
      fprintf(fpr2, "%c", a2[i];
      fprintf(fpr3, "%c", a3[i];
    }
    
    //then fclose all files
    it crashes when i run it, but there are no compilation errors. the funny thing is, when i change 'size' to a smaller number, say 10,000, it runs perfectly. i was told this might be due to some differences in the 'stack' and 'heap' memory types in the computer, which is why im now using malloc instead.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What do you mean when you say "numbers"?

    fscanf(%c) and fgetc() are going to be the same thing, except the latter is considerably less heavy on machine resources.

    > i was told this might be due to some differences in the 'stack' and 'heap' memory types in the computer
    Your default stack size is usually around the 1MB mark.

    If you really want to store the whole (and large file), then you'll need
    unsigned char *p = malloc( size * sizeof *p );
    Your first post was good, except for the lack of a size (you only had 1 char).
    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.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    Yea im using the word ‘numbers ‘ rather loosely here i think. Basically Im doing some image processing, whereby each of my image file contains 1,249,820 pixels and each pixel is represented by a number ranging from 0-255. So I presume that the data is stored as unsigned char since each number is 1 byte.

    Actually, how many data types are there? Ascii, binary, and text? And how do we determine which format a file is in?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I would still read the file in large chunks rather than 1 byte at a time, since it's much faster. After you have the chunk in memory you can do whatever you want with those bytes before you write them to the new file.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    unsigned char *b = malloc( size * sizeof(*b) );
    if ( b != NULL && fread( b, 1, size, fp ) == size ) {
      // yes, got the whole file in one go
    }
    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.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    Whats the difference between fwrite and fprintf? I tried the following variations:
    Code:
     
    for(i=0; i<size; i++)
    {
            fscanf(fp2, "&#37;c", &b2[i]);
            fscanf(fp3, "%c", &b3[i]);
            c[i]=((float)b3[i]-(float)b2[i])/((float)b2[i]+(float)b3[i]+0.000001);
            fprintf(fpndvi, "%f", c[i]);
    }
    and
    Code:
    for(i=0; i<size; i++)
    {
            fscanf(fp2, "%c", &b2[i]);
            fscanf(fp3, "%c", &b3[i]);
            c[i]=((float)b3[i]-(float)b2[i])/((float)b2[i]+(float)b3[i]+0.000001);
    }
        fwrite(c, sizeof(float), size, fpndvi);
    Both can run fine, but give vastly different results (that is what my image processing program, ENVI, tells me anw). I am quite certain im writing floating-point data to disk in both cases..

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    fprintf() prints the floating point value as digits that we humans can read. fwrite stores the raw (aka "binary") floating point data as it is stored internally in the memory, so 1.0 would be represented by 3F 80 00 00 or 00 00 80 3f - which you get depends on the type of processor being used.

    Which you should use depends on what the other side is using to read it back in. If the other side is using fscanf("%f" ... ) then you need to use fprintf. If the other side is using fread, then fwrite is the right way to go.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Searching Binary Files for a Pattern
    By CaptainMorgan in forum C Programming
    Replies: 16
    Last Post: 06-17-2007, 06:04 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Replies: 3
    Last Post: 03-31-2002, 11:03 AM