Haven't seen this problem in a few months. I understand your frustration because it's a very subtle problem to find! ;-)
This is a good example of why it's important to run a program through a debugger and carefully watch what happens.
The first clue is that all the files become corrupted. That often indicates that there's a problem with the file header - ...
It's failing because the program is terminating abnormally, even though it is creating the output files. When I run it, I get this, along with the recovered images:
~/go/test/pset4/jpg/ $ ./recover card.raw
munmap_chunk(): invalid pointer
This causes the tests to fail. The problem lies with this code:
Neither of these ...
This is a common error in this pset. Look at the following code closely.
fwrite(buffer, sizeof(BYTE), 512, img);
The pset spec says that the input file starts with garbage data, and to search for a signature. With that in mind, think about what happens when the first data block is processed. There is no signature and no ...
Look at this code:
// repeat until end of card
int block_size = 0;
unsigned char buffer[block_size];
This allocates 0 bytes to buffer. So, when the code attempts to write to buffer, there's nowhere to write to. How many bytes are supposed to be read in at a time?
No guarantees that there aren't other problems, but this should fix the seg fault.
The problem occurs when writing to an open file. You want to 512 to bytes using newfile, but you specify the write size to be of a BYTE. Change this to sizeof(newfile).
Hope this helps. If it does then please check the tickmark.
Actually, it's not working correctly. There's an infinite loop. It may look like it's working fine on your IDE, but try checking the file size of 049.jpg. It's growing without bound. I suspect your IDE is terminating the program when the disk space is being used up.
Take a close look at the code related to handling the EOF for the input file.