0
enter code here/**

* Recovers JPG files 512 B at a time . */

include

include

include

include

int main(int argc, char *argv[]) { // ensure proper usage if (argc != 2) { fprintf(stderr, "Usage: ./recover name\n"); return 1; }

// remember filename
char *infile = argv[1];

// open input file
FILE *card = fopen(infile, "r");
if (card == (NULL))
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}

int count=0; //JPEG counter
int o=0; //start sequence, if o = 1 open & write; if 0 = 3 close old, open & write new, if 0 = 2 write
typedef uint8_t  BYTE;
BYTE buffer[512]; //assign buffer size
char filename [8];
FILE *img;

while (fread (buffer, 1, 512, card) != 0x00) //While it is not EOF
{
    //found starting sequence
    if (buffer[0]==0xff && buffer [1]==0xd8 && buffer [2]==0xff && (buffer[3] & 0xf0)==0xe0)
    {
        o++; //indicator
         {
            if (o==1) //if the first starting sequence found
            {
                //open img file
                sprintf (filename,"%03i.jpg", count++);
                img = fopen( "%03i.jpg","w");
                o=2;
                fwrite (buffer,1, 512,img);
            }

            if (o==3) //if subsequent starting sequence found
            {
                //close previous img file
                fclose(img);
                //open new img file
                sprintf (filename, "%i.jpg", count++);
                img = fopen( "%i.jpg","w");
                o=2;
                fwrite (buffer,1, 512 ,img);
            }
         }
    }

    else
    {
        if (o==2) //if starting sequence found once, continue writing
        {
            fwrite (buffer,1,512,img);
        }
    }
}
printf ("Total number of JPEG is %i\n", count);

fclose (img);
fclose (card);

}

  • Without seeing ALL of the relevant code, it's impossible to see what's going on. I suspect it's something that's not in the image. Please edit the question, remove the screen capture image and then cut and paste in the actual code. It's impossible to test code from an image and nobody is going to try to transcribe code from an image. – Cliff B Nov 30 '17 at 12:14
  • Thank you Cliff. I have just edited as per advice. – Winy Nov 30 '17 at 12:20
  • Just a hint on code formatting: In the StackExchange editor, select the code and click on the {} button or press Ctrl+K. This will add four spaces in front of each line (unless there already were four spaces in front of each line, then it will remove them). Blocks of lines with four spaces in front are considered code, and rendered in a block with scroll bars, as you can see with your main function's content. Outside, for example #include <stdio.h> will be interpreted as a caption of level 1 (one hash) of text "include" and followed by a HTML tag. – Blauelf Nov 30 '17 at 12:45
  • ^ i have reformatted the code submission. Hope that's correct. TQ for the answer below and the tip above. – Winy Dec 1 '17 at 12:37
1

Use fopen(filename, "w"). That's why you used sprintf before.

Also, use %03i, not %i, in both cases.

| improve this answer | |

You must log in to answer this question.

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