When I run my program, only images 000.jpg through 012.jpg are recovered, and I can't figure out why. I've gone through the code over and over, checked everything, and I've added comments for everything to make sure I understand what everything does, but I can't fix this issue. So, what's going on?

Here is my code: https://github.com/jhschwartz/pset4/blob/master/recover.c


online 55, you're trying to put a string of length 6+ into a variable of size 3.

on line 104, you're declaring a variable called img that shadows the original variable called img (declared outside of the loop) and goes out of scope as the closing brace on 111 is reached. yet you're writing to it and trying to close it in next iterations.

the behavior of such operations is undefined and could cause your program to behave weirdly or even crash. also there are things that you don't really need. for example, you don't need an array of 4 elements. you could compare the first 4 elements from buffer with your arrays. also you don't need an enumeration. you could just use a bool variable, etc.

  • I'm not sure if I'm understanding this correctly. I can only keep a file open within one iteration? And I know that I declared the img once already before the big while loop, but I had to do this because I got compiler error for line 116 if the img file hadn't been opened outside the previous loop.
    – jhschwartz
    Jun 15 '15 at 1:22
  • Making the title string (line 55) size 8 instead of 3 recovered all the files even though I haven't yet changed line 104
    – jhschwartz
    Jun 15 '15 at 1:25
  • Also, are enumerations frowned upon? I kind of like them in the place of booleans. They sort of create more descriptive, unique booleans, no?
    – jhschwartz
    Jun 15 '15 at 1:27
  • @jhschwartz not really. a file could stay open until you call fclose or the program terminates. I never said the file is closed. it's the variable that you use to access (write to and close) that file that goes out of scope because it's declared locally inside the body of the if statement. see youtube.com/watch?v=UC5QAokAupo!
    – kzidane
    Jun 15 '15 at 1:40
  • @jhschwartz and it's not that enumerations are discouraged. it's that this is probably this is not the best situation where you should use an enumeration. IMO, a bool variable with a descriptive name (e.g., found) would suffice since you have only 2 possible values. that way you could just have something like if (found) instead of if (status == found_img). you also wouldn't need to worry about defining an enumeration, etc.
    – kzidane
    Jun 15 '15 at 1:44

You probably figured this out by now but just in case others are having a similar issue (I was).

Specifically re: only finding 13 images. I believe your issue has to do with the syntax in your if || in line 66. There are 13 images ending in 0xe0 and 3 ending in 0xe1. If you run individually you can see the same.

I corrected mine by using the below syntax - added parens around full statement on both sides of ||):

if ((memcmp(header0, buffer, 4) == 0) || (memcmp(head1, buffer, 4) == 0))

You must log in to answer this question.

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