0

I m struggling. Can't find why I have this error message

:~/workspace/pset4/recover/ $ ./recover card.raw
 Could not open new file.                        

Here is my code :

  //define data structure for having exactly 1 byte
typedef uint8_t BYTE;


//to store bytes of the size of an array = 512 =block size long  (size of an array of 512 which is 1 byte long) => store memory which bunch of 512 bytes called buffer
BYTE buffer[512];

//count for number of JPEG file - should get total of 50 with raw
int count = 0;
FILE *image = NULL;

//read the memory card until end of file (end of memory card) so no more 512 block left, where size of block is 512 from FAT
while (fread(buffer, 512, 1, cardfile) == 1)
{
    //check the first 4 bytes pattern to find the JPEG signature (fourth byte 0Xe (0 à 7 ou a à f))
    if (buffer [0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && (buffer [3] == 0xe0 || buffer[3] == 0xe1))
    {

        //if an outfile is open, close it to than start a new file
        if (count > 0)
        {
            fclose(image);
        }


        //if JPEG signature start a new file so create the file to be opened to be able to copy the JPEG image from memory card

        //create name of the file with char for jpeg filames with 3 digits => 000.jpg (7 caracters) + "\ 0" (+1 caracter) = total of 8caracters
        char filename[8];
        //create a name for each file
        sprintf(filename,"%03i.jpg", count);
        //increase number of filename for next jpg found => 000.jpg, 001.jpg. 002.jpg etc...

        //open the file
        image = fopen(filename, "w");
        count ++;

        if (image == NULL);
        {
            fprintf(stderr, "Could not open new file\n");
            return 3;
        }

         //write the new file so copy the memory card ofr one image at a time
        fwrite(buffer, 512, 1, image);
    }

    //if no JPEG signature, just copy on the file until closed or end of file (when found a new signature file will be closed)
    if (count > 0)
    {
        //write all the blocks after the JPEG signature
        fwrite(buffer, 512, 1, image);
    }

}
// close outfile each one at the time ( when encounter another signature)
fclose(image);

// close memory file
fclose(cardfile);


// success
return 0;
}
3
  • 1
    Putting 512 bytes into a 511 byte buffer?
    – Blauelf
    Commented Aug 24, 2017 at 7:32
  • tks for helping but still the same error ! Well with the [ ] with the 0 it does 511 and not 512.
    – Genevie
    Commented Aug 24, 2017 at 21:14
  • I got it : it s declaring the size of the array and not for accessing to an array
    – Genevie
    Commented Aug 25, 2017 at 1:29

1 Answer 1

0

I had to remove the image == NULL condition because new file will always open. tks folks

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .