The if statement given for recover.c for some reason keeps returning false and I'm not sure exactly what I'm doing wrong here.
---
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.
Sign up to join this communityOne thing that's wrong: char filename[1];
. How "big" is a file name? Bigger than 1. (and don't forget the null-byte, sprintf
creates a valid string, ie null terminated). That's not causing this problem, but it's a memory leak/buffer overflow waiting to happen.
An other (bigger) thing is: back-to-back reads. Here fread(buffer, MEM_BLOCK, 1, input);
then here while (fread(buffer, 1, MEM_BLOCK, input) == MEM_BLOCK);
. It's probably always returning false because it never finds a jpg sig.
An other (even bigger) thing is: int buffer[MEM_BLOCK];
. An int is length 4, so this is reading (and writing) 4 * MEM_BLOCK
bytes. You need to use a byte
type. Get a hint from bmp.h
in resize exercise.
A quick way to properly format your code, whether on initial post or edit: select the code, then click the {}
icon in the menu bar.
i
it to test; it still doesn't find anything.
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
?