0

My code is recovering 50 images, but according to check50 the images are not being recovered correctly. A few of the images are obviously not recovering but I can't figure out why in my code. Several of the recovered images are clearly showing the left side of the image on the far right as an example of the obvious issues.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{

//  Check to make sure that user submitted a file to recover images from
if (argc != 2)
{
    fprintf(stderr, "Invalid file\n");
    return 1;
}

// Establish the infile as the submitted file
char *infile = argv[1];

// Open input file
FILE *f = fopen(infile, "r");

// Verify that input file opened
if (f == NULL)
{
    fprintf(stderr, "Could not open %s \n", infile);
    return 1;
}

// Establish variables to support the while loop array
// Array of 512 bytes to store the data read from the card, array to store the filename, counter to keep track of how many JPEGs found, out file
BYTE buffer[512];
char *filename[7];
int counter = 0;
FILE *img;

// loop through the memory card in 512 byte chunks when number != 1 then end of file is reached
while (fread(buffer, sizeof(buffer), 1, f) == 1 )
{

    // Check if the read block is the start of a jpeg
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        // check if its the first jpeg
        if (counter != 0)
        {
            fclose(img);
        }

        sprintf(*filename, "%03i.jpg", counter);
        img = fopen(*filename, "w");
        fwrite (&buffer, sizeof(buffer), 1, img);

        counter++;
    }
    // Need to determine if a JPEG has already been found
    else if (img)
    {
        fwrite (&buffer, sizeof(buffer), 1 , img);
    }
}

fclose(img);
fclose(f);

return 0;

}

Can someone help me understand what could be causing my images to not recover correctly? I tried to debug but it's giving me an error when I try to step through the writing of the image files that I'm not getting when I just run it.

0

It's an interesting problem and I can understand why you can't find it. It's nowhere near where you'd think it is!

Here's the problem:

    char *filename[7];

The filenames are 7 chars long, but you still need to allow for the end of string marker, '\0'. When you create the filename strings, it overwrites the next physical byte in memory with that EOS marker. In this pset, it frequently happens to be the first byte of the buffer, so the first byte in the output file usually ends up being 0x00.

[Edit] In addition, it needs to be a char array, not a char POINTER array!

Try changing the size from 7 to 8, and remove the "*" and see what happens.

No guarantees that you won't have other problems.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • Yeah, I was playing around with that at 7 and 8 to see if that would change anything, happened to be on a [7] when I copied. Changing between [7] & [8] in this instance doesn't alter the image results. I understand it's just a coincidence though and I should use [8] in the future. Still looking for what is causing my images to be slightly off, top left and bottom right corners as well as transposing a small section of the left side of the image to the right are usually what's wrong – Sean May 13 '20 at 21:42
  • It's still an issue with the filename and how it's created. Try converting all of the references to filename to a char array instead of a pointer to an array. – Cliff B May 13 '20 at 21:59
  • Thanks that did the trick. I wasn't expecting that to have anything to do with the issue since the filenames were being created as I expected. I need to research pointers more as I don't quite get how that can cause a subset of pixel columns to get pushed to the other end of the img – Sean May 14 '20 at 1:23
  • @CliffB I had this exact same problem and this fixed it. Thank you so much, this it was driving me crazy! There was so much emphasis in the lecture about using char * that I just defaulted to using it. Can you explain why using char filename works but char *filename caused this issue? I want to understand it but from how I understood the lecture it seems like using a pointer to the contents of an array and using the array itself are supposed to be the same thing, no? Just that one has more versatility as it can be used beyond the scope of a function? – IB21 Jun 6 '20 at 6:39

You must log in to answer this question.

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