please help me with recover. this is my code.with check 50 i am getting this.

:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( recovers 000.jpg correctly
    000.jpg not found
:( recovers middle images correctly
    001.jpg not found
:( recovers 049.jpg correctly
    049.jpg not found

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
        fprintf(stderr,"Usage: ./recover image\n");
        return 1;
    char * name=argv[1];
    FILE * rec=fopen(name,"r");
         fprintf(stderr,"file opening error\n");
         return 1;
    FILE* outfile;
    int c=0;
    int found=0;    
    unsigned char buffer[256];
    if(buffer[0]==0xff  && buffer[1]==0xd8  && buffer[2]==0xff  && (buffer[3] & 0xf0 )==0xe0)

        char filename[8];
        outfile=fopen( filename,"a+");

    if (found==1)



1 Answer 1


For nicer formatting, look at the {} button in the question/answer editor. Select the code and click that button to indent it, this indentation by 4 spaces in front of every line is considered marking a code block.

handles lack of forensic image


If the forensic image cannot be opened for reading, your program should inform the user as much, as with fprintf (to stderr), and main should return 2.

Note the return code of 2, not 1, in that case.

other test cases

One mistake is 0%3d. In this case, 0 is taken literally, the number is still padded with spaces (which makes this string 8 characters long, plus null terminator requiring 9 bytes, too long for your buffer!). It would have to be %03d.

Also, a+ might work if your output file does not yet exist, but it will append to an existing file. Use wb (write binary) instead. http://man7.org/linux/man-pages/man3/fopen.3.html states

The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two- character strings described above. This is strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the 'b' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-UNIX environments.)

so w should be enough, but certainly not a+.

And in case the card image does exist, but does not contain any jpeg (not a test case as far as I know), you could add a protective if (found) in front of your second fclose(outfile).

You must log in to answer this question.

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