Fix segfault crash if output file cannot be opened