I'm having trouble reading and writing the contents of the JPG files in Pset4 recover.
When I run my program, I'm getting all 50 JPGs as files 000.jpg to 049.jpg. However, the pictures all appear to be checkered black and white boards of the same thing, though of various height and width. I'm trying to pinpoint whether my code is logically incorrect or whether there is just bug somewhere in my program.
I watched Zamyla's walkthrough and I know Zamyla divided the pseudocode into If at Start of a JPG, then do the following and If Already Found a JPG, then do the following. I'm confused on why I need the second clause of If Already Found a JPG. To me, fread reads 512 bytes at a time and we know the each JPG size is 512 and all JPGs are next to each other. Therefore, I believe my code is doing the following:
- while we are not at end of input file
- reads 512 bytes into buffer
- reads 512 bytes until finds matches first 4 bytes of JPG
- once jpg is found, sprints the title
- opens an img FILE* img = fopen
- if img is not null, fwrites what is in buffer into the img
- closes the img and add 1 to JPG count number
- steps 3 - 7 are all within one if condition. Once the condition is finished for the first JPG is over, the code goes back to the fread, which will read the next 512 bytes and do the same thing.
I understand that Zamyla's pseudocode may be more efficient (b/c you are not reading the entire first 4 bytes over and over again), but is it really necessary? If so, why? If not, this tells me that there's a bug somewhere within my if condition (steps 3 - 7)...
Thank you for your help!