1

I'm stuck on recover. My code runs and creates the files, but does it does not appear successful at recovering the pictures.

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

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Please enter no more or no less than one filename.\n");
        return 1;
    }

    char *infile = argv[1];
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    int i = 0;
    unsigned char buffer[512];
    while (fread(buffer, 512, 1, inptr)!=0)
    {
        if (buffer[0]==0xff && buffer[1]==0xd8 && buffer[2]==0xff && (buffer[3] & 0xf0)==0xe0)
        {
            char file_name[0];
            sprintf(file_name, "%03i.jpg", i);
            i++;
            FILE *outptr = fopen(file_name, "w");
            fwrite(buffer, sizeof(buffer), 1, outptr);
            fclose(outptr);
        }
    }
    fclose(inptr);
}
5
  • Take a closer look at your if (buffer[0]==... line... – ben-Nabiy Derush Jun 21 '17 at 17:59
  • Boy am I stupid sometimes. Thanks! – Alex Durante Jun 21 '17 at 19:02
  • However, while the code now runs, it does not appear to be recovering the pictures. Any other hints would be appreciated. I'll modify the post reflect that it is now a different problem. – Alex Durante Jun 21 '17 at 19:05
  • Problem has been resolved. I was missing a condition from the pseudocode. – Alex Durante Jun 21 '17 at 20:43
  • glad you could solve it! Happy coding! (Note: In the future, it would be good to not list so much code in a public setting, unless it is pseudocode or a mockup, as it could promote breaches in Academic Honesty) It is one of the difficulties faced when having a public help site like this for answers, but not to provide cheating answers to the people to come! – ben-Nabiy Derush Jun 21 '17 at 21:24
2

Note: This applied to the original question causing the code to not run. The question has since been modified...

Without disclosing too much:

In the if (buffer[0]==...

can you have buffer[1]== 2 different values in a string of &&'s ?

You must log in to answer this question.

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