0

I have been trying to figure out what has been the problem now for a while but I can't.

When I run GDB, it is telling me that compare[0] is either 97, 235, 82 or 0.

int counter = 0;
FILE* inptr = fopen("card.raw", "r");
BYTE compare[512];
fread(&compare, 512, 1, inptr);
while (!feof(inptr)) {
    if ((compare[0] == 0xff) && (compare[1] == 0xd8) && (compare[2] == 0xff) && (compare[3] == 0xe0 || compare[3] == 0xe1)) {
        char filename[9];
        sprintf(filename, "%03d", counter);
        sprintf(filename, "%s", ".jpeg");
        FILE* outptr = fopen(filename,"w"); 
        fwrite(&compare, 512, 1, outptr);
        fread(&compare, 512, 1, inptr);
        while ((compare[0] != 0xff) && (compare[1] != 0xd8) && (compare[2] != 0xff) && (compare[3] != 0xe0 || compare[3] != 0xe1)) {
            fwrite(&compare, 512, 1, outptr);
            fread(&compare, 512, 1, inptr);
        }       
        fclose(outptr);
        counter++;
    }
    fread(&compare, 512, 1, inptr);  
}
fclose(inptr);

I know compare[0 or 2] should be 255, compare[1] should be 216, and that compare[3] should be 224 or 225 but it won't show that. I also ran valgrind and nothing was wrong. Am I opening the file correctly or is there something wrong with fread?

1 Answer 1

0

Your program works fine, and it does find the first jpg, but these lines don't work the way you think they do:

sprintf(filename, "%03d", counter);
sprintf(filename, "%s", ".jpeg");

and you end up trying to make a file called ".jpeg". This line will clear it up:

sprintf(filename, "%03d.jpg", counter);

Then you'll get your first jpg without issues, but the placement of your reads and writes still isn't quite right, and the rest are still garbled.

Spend some time trying to see if you can find the issue, but here's a hint: What happens when your program finds the header for the next jpg?

1
  • Thank you so much for the comment :)
    – XxSaffxX
    Oct 5, 2015 at 2:18

You must log in to answer this question.

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