0

I am 3 days into PSET 4 Recover.

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 the header of a JPEG file has been found. The outfile should be opened and then written to. Once successful, 50 JPEG files should be returned, you can open them to check if the operation has worked.

My issue is that my program compiles successfully but does not return any JPEG files. There are no errors to point me in a direction of what the issue might be. My guess is there's a problem with the structure or logical flow of the program and certain parts of the program are not being reached. Does anybody have any suggestions?

typedef uint8_t byte;

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

// Global Variables
int picnumber = 0;
bool firstJPEG;
FILE* input = NULL;
FILE* outfile = NULL;

int main(int argc, char *argv[])
{
    // Checks that one command-line argument has been entered, if so opens memory card
    if (argc != 2)
    {
        printf("Usage: ./recover [___.raw]");
        return 1;
    }
    else
    {
        input = fopen(argv[1], "r");
        if (input == NULL)
        {
            printf("Could not open %s", argv[1]);
            return 1;
        }
    }

    byte block[512];

    // Loop through the file until no more data
    while (fread(&block, sizeof(block), 1, input) == 1)
    {
        // Search through file for JPEG headers
        if (isJPEG(block) == true)
        {
            // First JPEG header found, set first JPEG to true
            if (firstJPEG == false)
            {
                firstJPEG = true;
            }
            // Already a JPEG in the block. Close it
            else
            {
                fclose(outfile);
            }

            // Open and Print out recovered JPEGs into outfile.
            char filename[8];
            sprintf(filename, "%03i.jpg", picnumber++);
            outfile = fopen(filename, "w");
            if (outfile == NULL)
            {
                return 1;
            }
            fwrite(block, sizeof(block), 1, outfile);

        }

        // If finding another JPEG which is not the first, continue writing JPEGs to outfile
        else if (firstJPEG == true)
        {
            fwrite(block, sizeof(block), 1, outfile);
        }
    }
    // End of memory card - close remaining files
    fclose(input);
    fclose(outfile);
}

bool isJPEG (byte *block)
{
    // Scan block to see if it is a JPEG
    if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
  • @John Yang In line with your advice I have removed the return 0 from the first part of the program. Which would have seemed like an obvious problem. I have simplified it a little also. I have moved the initialization of the pointers to the beginning of the program. I also noticed there was an & missing from 'block' at the beginning of the While statement which I have corrected. The code has been revised above^ with still the same issue as before. Any other thoughts.? – Ciaran Egan Sep 3 at 9:51
  • @ John Yang UPDATE* It is working correctly!! Thank you for all your help! – Ciaran Egan Sep 3 at 10:06
1

I think the issue is with this line of code:

while (fread(block, sizeof(block), 1, input) == 1)

fread returns the number of items successfully read. So, if it successfully reads 1 block of data, it would return 512 and not 1. Since the condition isn't true, the while loop gets skipped and the program exits.

Edit: you are also returning 0 upon reading the input file, which would cause the program to exit immediately. In addition, your error checking for the command line arguments would cause problems with the compiler since you are initializing the input file pointer within the if condition.

Another edit: it appears that the line of code I initially stated isn't actually an issue.

| improve this answer | |

You must log in to answer this question.

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