0

Ok now for the last 48 hours I have been trying to make recover work. First I made a program that got only 8 correct images. I posted my problem in this link and got a response and understood how my code had two back to back freads Recover first 8 images but no Cosecutive freads

So then decided to code again with somehow using just a single fread and fwrite and wrote this code, with this I am only getting 1 image and the terminal is hanging. I am too tired and frustrated right now to even figure out why the terminal is hanging i guess its probably due to the loop. But plz can someone give me clear hints in easy english if I am on the right track and what to do next. Really wanna finish this course and this pset

    /**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
 */
#include <stdio.h>
#include <stdint.h>
#include <cs50.h>

int main(int argc, char* argv[])
{
    // open card file
    FILE* inptr = fopen("card.raw", "r");

    // check for errors
    if (inptr == NULL)
    {
        printf ("could not open card. \n");
        return 1;
    }

    // declaring a block of 8 bits of Type BYTE and having 512 blocks
    typedef uint8_t BYTE;
    BYTE block[512];
    FILE* picture ;
    int counter = 0;
    while (fread(&block, 512, 1, inptr) > 0)
    {








        // checking for headers of JPEGs

        if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] == 0xe0 || block[3] == 0xe1))
        {
            char name[9];
            sprintf(name, "%03i.jpg", counter);
             picture = fopen(name, "w");





            while (true)     

            {

                fwrite(&block, 512, 1, picture);
                if (!(block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] == 0xe0 || block[3] == 0xe1)))
                {
                    break;
                }

            }



        fclose(picture);
        counter++;
        }


    } 

  fclose(inptr);  

}

1 Answer 1

0

Take a look at where you placed the closing brace for the line:

 `if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] == 0xe0 || block[3] == 0xe1))`

You don't exit it to make the next iteration of fread so you're not advancing to the next block.

You must log in to answer this question.

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