1

Does anyone know why I might be receiving this error in my recover program? I've used xxd to confirm that the first four bytes are correct and signal that each is a jpeg. However, I am still receiving this error.

while fread(512 bytes from src into buffer) does not return 0
    if(A JPEG is found)
        char title[8]
        sprintf(create new file)
        img = fopen(title, "w")
        fwrite(512 bytes from buffer into img)

    if(eof)
        fclose(src)
        fclose(img)

1 Answer 1

5

It appears that you're not recovering the JPGs correctly. I assume that you declared img beforehand, but actually when you open it, you should open it with the mode a not w.

The a mode opens a file for appending (writing at end of file) while the w mode overwrites the data in the opened file (if exists). Both, however, create the file if it doesn't exist.

Also, you're writing only the first 512 bytes of the JPG while you should write until another start of a JPG is found or the end of file is reached.

Keep in mind that feof() returns true after the EOF is reached not before (i.e., it doesn't check whether the EOF is the next to be read, but rather, it checks if it was already reached).

3
  • Thanks so much it is recovering the images correctly. Now I just have a check50 error of "\ expected an exit code of 0, not output of "��������\u0000\u0010JFIF\u0000\u0001\u0001\...", which I think has something to do with using fclose. Im just having trouble figuring out when to do so. Jul 22, 2014 at 0:46
  • @IanEdwards not really, if your program exits normally, you should return 0 upon exiting, otherwise, you should print a message telling the user what the error is and return a number to indicate that error! Your program shouldn't output anything other than this!
    – kzidane
    Jul 22, 2014 at 1:11
  • @Ian Edwards, I have the same problem with the seemengly random input (starting with question marks). How did you solve this problem? I can recover all the jpgs normally, though. Just the check50 doesn't accept it. Nov 16, 2014 at 21:27

You must log in to answer this question.

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