Use CFile in a few more places

CFile prevent resource leaks by always closing the FILE object via the
destructor. Improves readability too, by not cluttering the code with
fclose calls.

Also fixes a memleak in jpeg_save, when file failed to open the jpeg
encoder wasn't destroyed.