You can use fopen()
. Seriously, don't take any notice of Microsoft here, they're doing programmers a real disservice by deviating from the ISO standards. They seem to think that people writing code are somehow brain-dead and don't know how to check parameters before calling library functions.
If someone isn't willing to learn the intricacies of C programming, they really have no business doing it. They should move on to a safer language.
This is just another attempt at vendor lock-in by Microsoft of developers (although they're not the only ones who try it; I'm not specifically berating them). I usually add:
#define _CRT_SECURE_NO_WARNINGS
(or the "-D"
variant) to most of my projects to ensure I'm not bothered by the compiler when writing perfectly valid, legal C code.
Microsoft has provided extra functionality in the fopen_s()
function (file encodings, for one) as well as changing how things are returned. This may make it better for Windows programmers but makes the code inherently unportable.
You can tell from the prototype why they've changed this one. The fact that they're returning an errno
and setting the file handle using double indirection almost certainly indicates that they think the real fopen()
has a threading issue with the global errno
:
errno_t fopen_s(
FILE** pFile,
const char *filename,
const char *mode
);
Why they didn't just define errno
as a function call (to return a thread-specific error) or a field in a thread-specific data structure like every other implementation is beyond me.
Aside: According to some comments, they do define errno
to be thread-specific. This seems to indicate that the sole purpose of the "safe" version is the extra functionality.
If you're only ever going to code for Windows, by all means use it. I myself prefer the ability to compile and run my code anywhere (with as little change as possible).