1

Hey guys so I've hit a bit of a wall with Recover, when I run my program I get segmentation fault but I also don't know how to end off one jpeg when another header pops up? In essence, I'm not sure what I could use to close the jpeg and restart the process when another header is found.

My code:

//declare crucial variables
BYTE buffer[512];
int img_counter = -1;
char *flname = "0.jpg";
FILE *img = NULL;

//while loop to go through the card
while (fread(&buffer, 512, 1, card_file))
{
    //if first 4 bytes of block = jpeg header, begin writing process. Otherwise keep writing to current jpeg
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {
        img_counter++;
        sprintf(flname, "%03i.jpg", img_counter);
        img = fopen(flname, "w");
        fwrite(&buffer, 512, 1, img);
    }
    else
    {
        //not sure what to put here to write the block with no headers in them to the jpg

    }
}

//close the card after use (end the program)
fclose(img);
fclose(card_file);
return 0;

Thanks in advance for the help!

Kind Regards
PrimeBeat

1

char *flname = "0.jpg";

The string here is immutable. Writing to it again is undefined behavior. You need to stack allocate space for a file name. 000.jpg is 7 characters and you need one extra byte to store the null terminator so 8 characters will do it. char flname[8]; You almost had it.

In essence, I'm not sure what I could use to close the jpeg and restart the process when another header is found.

Well, you have a pointer to the current image. And that pointer starts out as NULL. So when you know you're going to open a new image, first you can check if in image is already open, and then close it.

if (img != NULL)
{
    fclose(img);
}
...
img = fopen(...);
else
{
    //not sure what to put here to write the block with no headers in them to the jpg
}

The else branch will always execute when the if branch doesn't. But are you sure that's what you want? There are 3 different scenarios here.

  1. It's the start of a new image, in which case you want to open a new image, and potentially close the old one.

  2. An image is open img is not NULL. So then you just want to write to the open image.

  3. It's not the start of a new image and no image is open, so do nothing and keep reading the memory card.

The thing to note here is, 1 and 2 are not actually mutually exclusive. The only condition for writing to an image is that an image is open. So why do you need an else branch? What you really want, is another if branch to check if an image is open.

5
  • so, that if statement you added in your answer could be used to check for an open image or am I misunderstanding you? – PrimeBeat Apr 22 at 10:17
  • @PrimeBeat I think you got it. If (img != NULL) If img is not null, that means an image is open, so it's safe to write to it. – Fuelled_By_Coffee Apr 22 at 10:24
  • Should I add the fwrite() function in that if statement then? – PrimeBeat Apr 22 at 10:28
  • @PrimeBeat yes. – Fuelled_By_Coffee Apr 22 at 10:29
  • Thx so much, the seg fault is solved and I get 50 jpgs. But now there are multiple valgrind errors and the jpgs don't open (won't even download). But I'll make a separate question for this – PrimeBeat Apr 22 at 10:34

You must log in to answer this question.

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