0

I am getting all 49 images, on check50 i get the message that the 049.jpg is mismatch.I am trying to debug, but i am getting a seg fault error.

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



int main(int argc, char *argv[])
{
uint8_t buffer[512];            // 512 bytes buffer
size_t numElem = 0;             // num of elem in fread
int newImg = 1;                
char *filename;                 // pointer to filename
int fileNumber = 0;             // stores file number
FILE *img;                      // pointer to file
filename = malloc(8 * sizeof(char)); //allocate filename

if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}

char *infile = argv[1]; // name of the input file

FILE *inptr = fopen(infile, "r"); // open the file to pointer

if (inptr == NULL)
//checking to see if the file is good
{
fprintf(stderr, "Could not open %s.\n",infile);
return 1;
}

do
{
// reading the first 512 byes of file
numElem = fread(buffer, sizeof(uint8_t),512,inptr);

if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        if(newImg == 0)
        //if its not a new image
        {
            fclose(img);
            newImg = 1;
        }
        sprintf(filename, "%03i.jpg",fileNumber);
        img = fopen(filename,"w");
        fwrite(buffer, sizeof(uint8_t), 512, img);
        newImg = 0;
        fileNumber++;
    }
else
    {
        //write to same image if not header bytes
    fwrite(buffer, sizeof(uint8_t), 512, img);
    }
}
while(numElem == 512);

fwrite(buffer,sizeof(uint8_t),numElem,img);
fclose(img);
fclose(inptr);
free(filename);
free(img);
free(inptr);
return 0;
}
0

Revise the logic in your loop. Try to think what happens when numElem gets other than 512 when reaching the end of the file......

You must log in to answer this question.

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