0

Since my program compiles fine I am wondering why nothing seems to be outputted from it. I looked at a few threads, but the only close question I found was here

Pset4 recover.c compiles, but can't seem to print Jpgs at all

But that didn't turn out very helpful since it was pretty specific for the questioner's code. Basically my question is the same however.

Here is the program: (top of code is getting cut off for some reason)

// check if image is not NULL, if it isn't open the file
if (argv[1] == NULL) 
{
    fprintf(stderr, "NULL image -Werror\n");
    return 2;
}
// else open file as normal --> first error was that cannot have initialized in if, must be for sure, etc
FILE *imageFile = fopen(argv[1], "r"); //and if so why doesn't this work?

// create unsigned in BYTE and put buffer in it of 512 bytes (as per JPEG size window)
typedef uint8_t BYTE;
BYTE buffer[512]; 

// creates file array to open and image to place it in
char fileArray[8];
FILE *img;

// counter to name each new JPEG file and bool to check if this is the first JPEP to be opened
// diff is that none needs to be closed if it is first
int fileCount = 0;
bool firstJPEG = true;

// ok this was one issue that have to put at buffer line to show that reading from to the actual file
while (fread(&buffer, 512, 1, imageFile) == 1)
{
    //fread(buffer, 512, 1, image); actually already reads the file ty cb

    // check if this is the beginning of JPEG using bitwise & (a bit confusing nevertheless)
    if (buffer[0] == 0xff && 
        buffer[1] == 0xd8 && 
        buffer[2] == 0xff &&
        (buffer[3] & 0xf) == 0xe0)
    {
        // only difference between checking between first or non first JPEG is not closing
        // the ending file and opening new one for first, just open new and set bool firsJPEG to false
        if (firstJPEG)
        {
            sprintf(fileArray, "%03i.jpg", fileCount);
            img = fopen(fileArray, "w");

            fwrite(imageFile, 512, 1, img);
            firstJPEG = false;
            fileCount++;
        }
        else 
        {
            fclose(img);

            sprintf(fileArray, "%03i.jpg", fileCount);
            img = fopen(fileArray, "w");
            // remember we created this image as one, not the whole image as before
            fwrite(&buffer, 512, 1, img);
            fileCount++;
        }
    }
    // even if we do not encounter new JPEG we need to write following 512 bytes to current file, but if
    // is new then can discard (only at beginning for file)
    else if (!firstJPEG)
    {
        fwrite(imageFile, 512, 1, img);
    }
}
fclose(imageFile);

// success!
return 0; 
1

The program never finds a jpg signature because this test (buffer[3] & 0xf) == 0xe0) is wrong. Suggest you review this answer by @Cliff B for an excellent explanation.

After you sort that out, there's other problems brewing with some of the fwrites. After you fread into buffer and find a signature, this fwrite(imageFile, 512, 1, img); will write out the next 512 bytes in image file, because that's where the file pointer is. It will not write the signature block at all (which is sitting in buffer, hint hint). Looks like that problem exists multiple times.

1
  • Thankee, I can't believe that I missed the syntax on that one! No wonder I was confused what to put in that first slot in fwrite. thanks a bunch Jun 14 '17 at 22:57

You must log in to answer this question.

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