0

I feel like I have been able to implement the majority of the pseudocode but I am struggling to think of a way to find the first jpeg. I thought my code would work but it doesn't as buffer[0]... etc only checks for the very first in the code, rather than allowing me to loop through and check each 4 bytes. Sorry if this is a dumb question but could anyone offer me any hints as to how to go about this? I have been struggling with this for a while.

Here is my code:

int main(int argc, char *argv[])
{
    FILE *output;
    char filename[55];
    //set no of jpeg files to 
    int nojpeg = 0;
    int jpegs = -2;
    //if too many/too little
    if (argc != 2)
    {
        printf("./recover IMAGE\n");
        return 1;
    }

    //open file
    FILE *fPointer = fopen(argv[1], "r");
    //check valid
    if (fPointer == NULL)
    {
        printf("Could not open file\n");
        return 1;
    }
    
    //in order to store file data in
    BYTE buffer[512];

    //until end of file
    while(fread(buffer, 512, 1, fPointer) == 1)
    {
        //HOW TO CHECK IF IT IS FIRST JPEG?

        //if they are JPEG...
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3] & 0xf0) == 0xe0))
        {
            //increase no of jpegs
            jpegs++;
            //read rest of block
            fread(buffer, 508, 1, fPointer);
            //make filename
            sprintf(filename, "%03i.jpg", jpegs);
            //if first jpeg
            if (jpegs == 0)
            {
                //open output
                output = fopen(filename, "w");
                //write into output
                fwrite(buffer, 512, 1, output);
            }
            //if not first
            else if (jpegs > 0)
            {
                //close previous img
                fclose(output);
                //open output
                output = fopen(filename, "w");
                //write into output
                fwrite(buffer, 512, 1, output);
            }
            
        }
        //if not the start of jpeg
        else if (jpegs > 0)
        {
            //read rest into buffer
            fread(buffer, 508, 1, fPointer);
            //open output
            output = fopen(filename, "w");
            //write into output
            fwrite(buffer, 512, 1, output);
        }
    }
    fclose(output);
}

Thanks!

0

I figured it out. I didn't realise that you can check for each jpeg at the start of each 512 block, so I just had to make a small adjustment to the code and it worked (the finding first jpeg, at least).

0

But didn't solve the pset. right ?

3
  • This does not provide an answer to the question. To critique or request clarification from an author, please leave a comment below their post. Jul 31 at 10:57
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
    – Vsjain
    Jul 31 at 11:25
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Cliff B
    Aug 18 at 16:08

You must log in to answer this question.

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