Fixed resource leaks when an invalid encoding was passed

When an invalid encoding string was passed, fin and fos could not close.