Wrap fopen()/fopen_s() calls to avoid duplication

This removes the duplication of numerous ifdefs, "mode" parameter string literals and other wiring around calling fopen/fopen_s by wrapping those into a helper function.