0

I've recovered all 50 JPEGs and when I download them to my computer and change the name from (for example) 000.jpg%OA I can see the picture correctly. However, in the IDE,if I open the same JPEG I get hundreds of lines of garbage code. Any idea why I can't see the JPEGs in the IDE and can't pass the check50?

my code:

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

int main (int argc, char *argv[]) {
typedef uint8_t HEXHOLDER;
int foundPhoto = 0;

if (argc != 2) {
     fprintf(stderr, "Please enter one command line argument\n");
     return 1;
}

char *infile = argv[1];
char *outfile = malloc(sizeof(char) * 8);



FILE *inptr = fopen(infile, "r");

if (inptr == NULL) {
   fprintf(stderr, "It looks like the file you entered does not exist. Please try again.\n");
   return 2;
}
HEXHOLDER buffer[512];
FILE *outptr = NULL;


while (fread(buffer, 1, 512, inptr) == 512)

{
    fseek(inptr, -512, SEEK_CUR);
    fread(buffer, 1, 512, inptr);



    if (buffer[0] == 0xff &&
        buffer[1] == 0xd8 &&
        buffer[2] == 0xff &&
        (buffer[3] & 0xf0) == 0xe0) {

        if (foundPhoto == 0) {

            sprintf(outfile, "%03i.jpg\n", foundPhoto);
            outptr = fopen(outfile, "w");
            fwrite(buffer, 1, 512, outptr);
            foundPhoto++;

        }

        else if (foundPhoto > 0) {
            fclose(outptr);
            sprintf(outfile, "%03i.jpg\n", foundPhoto);
            outptr = fopen(outfile, "w");
            fwrite(buffer, 1, 512, outptr);
            foundPhoto++;

        }

    }

    else if (foundPhoto > 0) {

        fwrite(buffer, 1 , 512, outptr);

    }

    else {

    }


}

return 0;

}

Thanks a lot for your help!

2

Can you give me an example of a filename that is supposed to contain a line feed?

        sprintf(outfile, "%03i.jpg\n", foundPhoto);

As a side note, a programming tip. When you see to nearly identical blocks of code, maybe there's a way to rewrite it more efficiently, using only one block of code. Especially true when there are two blocks of code that are exactly identical.

if(file already opened)
{
    close file;
}

open new file;

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

  • Thanks for your help and advice! – George Wright May 18 '17 at 11:47

You must log in to answer this question.

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