Is there a way to use fopen_s() with GCC or at least create a #define about it?

Viewed 32046

MSVC compiler says that fopen() is deprecated, and recommends the use of fopen_s().

Is there any way to use fopen_s() and still be portable?

Any ideas for a #define?

6 Answers
#define fopen_s(fp, fmt, mode)  ({\
    *(fp)=fopen( (fmt), (mode));\
    (*(fp) ) ? 0:errno;\
})

Accroding to https://en.cppreference.com/w/c/io/fopen it's possible to enable *_s functions on the standard library:

As with all bounds-checked functions, fopen_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdio.h>.

Related