I've gotten recover to work... sort of. The problem is difficult to describe without posting my code, but here's an attempt:

My program can find all the jpg signatures (in GDB, my jpeg-finding line returns yes each time) but it only creates 2 jpg files, both of which are viewable images. The first file seems fine but the second contains all of the rest of the data to EOF.

From trying to debug in GDB, I believe the problem is in the implementation of my file naming scheme. The first file is called correctly called 000.jpg and the second file is correctly called 001.jpg (hooray!) but when it comes time to create the third file, it also calls that one 001.jpg and thus just appends the new jpg to the old jpg file.

I'm creating the filenames with sprintf, using a counter that is initially set to 0 like this:

int counter = 0;

and incremented after the previous filename is created, like this:


Then I tried initializing the counter to 5 instead of 0. That resulted one jpeg named 005.jpg and a second one called 001.jpg so it seems that when I increment the counter it only ever sets the counter equal to 1.

Give that limited information, anyone have an idea what's going wrong?

  • When you find an answer to your own question, it's encouraged to post that as an answer instead of editing it into your question. It's also fine to accept your own answer. This shows other readers what worked for you.
    – Air
    Jul 21, 2014 at 15:55
  • 1
    Apparently, you aren't permitted to answer your own question until it has been posted for 8 hours. The error message from Stack Exchange told me to try just editing my question to include my answer... Now that the requisite time has passed, I'll post my answer as an answer. Thanks for reading my question! Jul 25, 2014 at 0:14
  • You might be interested to know that the 8 hour delay has been eliminated, partly thanks to this question. :)
    – Air
    Sep 4, 2014 at 19:38

2 Answers 2


The problem turned out to be a memory allocation issue. I had declared another variable with too small an array size right before I declared my counter variable, so the first variable started running over into the second variable. Increasing the size of the first variable fixed the problem.


Sounds like you are declaring the counter as int counter = 0; each time around the loop.

If that's the case, make sure you only declare it once, outside the loop!

You must log in to answer this question.

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