Check return of fopen() before using it

Fix #9968 

@DeepDiver1975 @schiesbn @icewind1991