2

The 049.jpg file is created but not recovered! I have used the fread function to detect the EOF as told in the walk through by Zamyla.

Here is a link to the check50 sandbox output.

#   Checks  
0   Running...  clang -o recover recover.c -lcs50 -lm. ✔
1   Expecting an exit code of  0. ✔
2   card.raw. ✔
3   Running...  ./recover. ✔
4   Expecting an exit code of  0. ✔
5   Checking for file  049.jpg. ✔
6   TODO.   TODO
  • Are you closing the last recovered jpg file? – Kareem Aug 31 '14 at 9:34
  • Yes, Kareem I did! – Chelian Aug 31 '14 at 12:15
  • Sounds like you are closing the file before writing to it the last part. Showing us your loop (I assume) condition could be helpful. – Ckone2000 Aug 31 '14 at 12:30
  • @Chelian is 049.jpg correctly viewed? – Kareem Aug 31 '14 at 12:59
  • @Ckone2000 my loop condition is while(fread(&buffer,sizeof(BYTE),512,inptr) – Chelian Aug 31 '14 at 14:38
2

After looking at your source code file, here are my takes on it

  1. I don't really know why you're wrapping the array of BYTEs by a struct. All you probably need is the array by itself.

  2. The maximum length of the name of a JPG file is 7. Meanwhile, you only need a char array of length 8 (the additional index is for the null terminator) not 10 to store the name of a file .

  3. On line 41, no need to cast the values to BYTE.

  4. The main problem is apparently cause by not closing the output file and exiting the loop immediately after the EOF is reached, but rather writing what's in buffer one more time. You may test whether the EOF "was" reached already using a function like feof(). Execute

    man feof
    

in the terminal for more information about that function!

| improve this answer | |
  • I have exactly the same problem, I think. I recover and can view 049.jpg just fine, but check50 says it doesn't recover. Looking at the above, I'm not sure I understand the solution. I have a write loop that closes the jpeg file after every 512 bytes are written from the buffer, and opens it again if there's more to be written. And, everything is in a while(feof(card)==0) loop, which closes the card when not satisfied so, I think I'm pretty safe in saying that my problem isn't that I don't close the last output file... what might it be? – postylem Sep 3 '14 at 16:49
  • While(feof()) only returns true one loop after the EOF. What you want is the loop to stop executing once the EOF is reached, not one execution later. Therefore, you need an additional condition within your while(feof()) loop to ensure that no additional 512 bytes block is added to 049.jpg after EOF is reached. One option is given to you by Zamyla: Add an if statement with an fread() function as its condition, such as if(fread(&buffer, sizeof(BYTE), 512, infile) != 512), followed by a break statement. – pat_krat Sep 3 '14 at 17:01
  • I see. I didn't get that I had to terminate immediately, but now I see why that's obvious. Thank you @Patrick – postylem Sep 3 '14 at 17:30
  • @postylem did Patrick answer your question? Do you still have the problem? – Kareem Sep 3 '14 at 20:07
  • sorry, @Kareem, yes, Patrick answered my question. – postylem Sep 8 '14 at 12:08

You must log in to answer this question.

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