I'm having a problem with my code for recover.c because it recovers only 1 JPEG and that JPEG isn't a JPEG. It starts with 0x68 and 0x74.

After going through the code with GDB, it seems as that on the 1st iteration of the loop 512B is written to the JPEG even though the value of counter is 1 (it should only execute when counter != 1 as seen on line 57 in the screenshot below). I do not know why this is the case.

Here is the code that I used originally:


P.S. Could there be something wrong with the way I am checking for EOF? (Just wondering)

I tried fixing the part where the code only wrote 512B into the 1st JPEG and not continue writing into that JPEG. Here's the code with those changes:


Even so, the same problem exists. Running recover.c only recovers 1 JPEG which isn't a JPEG. (It doesn't have the signature.) Running recover.c with GDB yields the same problem as before: In the image above (line 57 of GDB) the condition if (counter != 1) executes even on the 1st iteration of the while loop, when counter == 1. Again, I am puzzled as to why this is happening.

2 Answers 2


The solution, according to the original poster (moved here from question body):

I finally solved the problem and now at least I am able to recover 2 complete images and 1 image that shows an error. Turns out that I didn't allocate enough space for my char array; I allocated 7 chars for my char array when in fact 8 were required. (I totally forgot about that \NULL char)


First, I would change the condition of your outer loop from while(true) to while(!feof(inptr)) plus an additional condition inside that loop, so your programme knows when to stop executing.

Second, to the logic of your programme, the reason you only get one 512 array of bytes written into your jpeg is your condition

if (counter != 1)
// write more arrays into the jpeg

Once you have found a jpeg you write the first 512 byte array into img, but you don't update counter, so you will never enter above named condition, leaving your jpeg with 512 bytes.

Change that condition to a simple


statement. This means that your programme does whatever you tell it to do once you find a jpeg signature, ELSE it keeps on writing 512 byte arrays into your img. Hope this helps.

  • Oh ok. There's a flaw in the logic in the code. But I don't understand why is it that when debugging my code on the very 1st iteration of the loop when counter == 1 the condition if (counter != 1) still executes. Because this condition executes, 512B are written to the JPEG even though they are not JPEG signatures.
    – user1742
    Sep 14, 2014 at 14:05
  • By the way, instead of changing that condition to an "else" condition I placed another while loop after locating the JPEG signature of the 1st JPEG because I wasn't very sure of the "else" condition plus I thought that the "else" condition would result in writing 512B to a JPEG regardless of whether JPEG signatures had already been previously found. Here's the edited version: gist.github.com/ehahehah/4b4038f7da99ab484b86
    – user1742
    Sep 14, 2014 at 14:08
  • gist.github.com/PatrickKratsch/b11cdd9cab31e451f49d I've added comments where necessary. I removed the while(true) statements, removed the second BYTE declaration, and added a condition to the else statement so it does not execute before the first jpeg is found. Does that make sense? The code is NOT FINISHED though. E.g. the while(!feof(inptr)) loop is insufficient to recover the last image as you will see. Let me know whether this helped.
    – pat_krat
    Sep 14, 2014 at 21:45
  • As to your debugging problem, I haven't used GDB on it myself, so I'm unsure why that condition would have been met..
    – pat_krat
    Sep 14, 2014 at 21:46
  • I just saw that I missed out on one curly brace after the second if statement. However, you can combine the two if statements for finding a jpeg signature into one anyway!
    – pat_krat
    Sep 14, 2014 at 21:48

You must log in to answer this question.

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