0

I am 2 days into PSET 4 Recover. I can get the program to compile successfully without errors but my program is not returning any JPEGs, with no errors to point me in any direction as to what might be the issue. My guess is there's a problem with the logical flow of the program and certain parts of the program are not being reached. Does anybody have any suggestions?

In PSET 4 we are supposed to create a program which inputs a memory card and scans through that memory card for the signatures of lost JPEG files. Once found, these JPEG files then need to be saved to new files to be recovered. There are 50 in total.

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

typedef uint8_t byte;

//Function prototype
bool isJPEG(byte *block);

int main(int argc, char *argv[])
{
    // Global Variables
    int JPEGfound = 0;
    int picnumber = 1;
    char filename[8];

    // Variable Initialization
    FILE* img = NULL;
    FILE* input = NULL;


// Checks that one command-line argument has been entered, if so opens memory card
    if (argc == 2)
    {
        input = fopen(argv[1], "r");
        return 0;
        if (input == NULL)
        {
            printf("Could not open %s", argv[1]);
            return 1;
        }
    }
    else
    {
        printf("Usage: ./recover [___.raw]");
        return 1;
    }
    byte block[512];
    // Loop through the file searching for JPEGs until no more data
    while (fread(block, sizeof(block), 1, input) == 1)
    {
        // Iterate through file searching for JPEG headers
        if (isJPEG(block) == 0)
        {
            // First JPEG header found, set JPEG found to 1
            if (JPEGfound == 0)
            {
                JPEGfound = 1;
            }
            // Already a JPEG in the block. Close it
            else
            {
                fclose(img);
            }

            // Filename defined by picnumber, iterates with new pn. Open new and write first JPEG inside
            sprintf(filename, "%03i.jpg", picnumber);
            img = fopen(filename, "w");
            fwrite(&block, sizeof(block), 1, img);
            picnumber++;

            // If JPEG has been found continue writing JPEGS inside
            if (JPEGfound == 1)
            {
                fwrite(&block, sizeof(block), 1, img);
                picnumber++;
            }

        }
    }
    // End of memory card - close remaining files
    fclose(input);
    fclose(img);

    return 0;
}

bool isJPEG (byte *block)
{
    if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
0

Because of the way the code is structured, the signature data blocks are being written to the output file twice - first in the code block that opens the file, and then again in the latter if (JPEGfound == 1) code block.

Also, note that the first file should be 000.jpg, not 001.jpg.

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

| improve this answer | |
  • That does sound like an error in the structure. I have removed it but am still finding the program not recovering any JPEGs. I have also corrected the picnumber to begin at 0. Unfortunately still unresolved – Ciaran Egan Aug 31 at 9:12
  • Without seeing the updated code, I can't help – Cliff B Aug 31 at 16:46
  • cs50.stackexchange.com/questions/39488/cs50-pset4-logic-problem. Ive linked an updated version of the code. Ive made some changes today but the x2 fwrites are still present with some clearer pseudocode/comments about what im trying to get it to do. – Ciaran Egan Aug 31 at 20:52

You must log in to answer this question.

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