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

fopen create file
fopen a+
fopen wb
fopen_s error 22
fopen_s error 13
fopen shared access
fopen_s file path
errno_t

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?


Microsoft's *_s functions are unportable, I usually use equivalent C89/C99 functions and disable deprecation warnings (#define _CRT_SECURE_NO_DEPRECATE).

If you insist, you can use an adaptor function (not necessarily a macro!) that delegates fopen() on platforms that don't have fopen_s(), but you must be careful to map values of errno_t return code from errno.

errno_t fopen_s(FILE **f, const char *name, const char *mode) {
    errno_t ret = 0;
    assert(f);
    *f = fopen(name, mode);
    /* Can't be sure about 1-to-1 mapping of errno and MS' errno_t */
    if (!*f)
        ret = errno;
    return ret;
}

However, I fail to see how fopen_s() is any more secure than fopen(), so I usually go for portability.

fopen - C++ Reference, or sign in using: Opens the file whose name is specified in the parameter filename and on the stream and how these are performed are defined by the mode parameter. The running environment supports at least FOPEN_MAX files open "w+", write/update: Create an empty file and open it for update (both for input  errno_t fopen_s(FILE *restrict *restrict streamptr, const char *restrict filename, const char *restrict mode); (2) (since C11) 1) Opens a file indicated by filename and returns a pointer to the file stream associated with that file. mode is used to determine the file access mode.


if you are using C11, fopen_s is a standard library:

http://en.cppreference.com/w/c/io/fopen

in gcc you need to use --std=C11 parameter.

fopen_s not resolved under ubuntu - c++ - android, I want to open file so I use fopen_s function under ubuntu. #endif reference: Is there a way to use fopen_s() with GCC or at least create a #define about it? @lurker: Where are you using GCC? The problem is actually in the C library. If you're using GCC on Windows and it is set up to know about the functions in the Microsoft C RunTime (CRT), then you'd be able to use them. If you're not on Windows, the functions are not generally available in the local C library.


In C/C++ code,

#ifdef __unix
#define fopen_s(pFile,filename,mode) ((*(pFile))=fopen((filename),(mode)))==NULL
#endif

In Makefile

CFLAGS += -D'fopen_s(pFile,filename,mode)=((*(pFile))=fopen((filename),(mode)))==NULL'

Attention that on success fopen_s return 0 while fopen return a nonzero file pointer. Therefore it is necessary to add "==NULL" to the end of macro, e.g.:

if (fopen_s(&pFile,filename,"r")) perror("cannot open file");

[PDF] Using the GNU Compiler Collection (GCC), the compilers included in GCC are implemented this way; they all generate The ISO C standard defines (in clause 4) two classes of conforming 38. Using the GNU Compiler Collection (GCC) int f(). { int i = i; return i;. } This option is intended to warn when the compiler detects that at least a return fopen (str, mode);. However, the solutions they're providing aren't portable. Anyway, if you aren't interested in using the secure version of their calls (like fopen_s), you need to place a definition of _CRT_SECURE_NO_DEPRECATE before your included header files. For example: #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h>


Many of Microsoft's secure functions are included in Annex K of the C11 standard, but it is not widely supported, so portability is still an issue. There is a need for the improved security in some applications; maybe the support will improve in the future.

I the past, I did it like this:

  #define fopen_s(fp, fmt, mode)          *(fp)=fopen( (fmt), (mode))

The macro is simple and straight forward, good enough for something quick and dirty, but it doesn't provide the exception behavior of fopen_s, and it won't provide the security of the real fopen_s function.

@Alex B's function approach above partially reproduces the proper behavior on failure; he returns errno (= EINVAL). His approach could be extended further by generating an invalid parameter exception to more fully reproduce the behavior of fopen_s.

fopen, fopen_s, 1) Opens a file indicated by filename and returns a pointer to the file stream is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the "a+", append extended, Open a file for read/write, write to end, create new In update mode, implementations are permitted to use binary mode even  As with any OS, file handling is a core concept in Linux. Any system programmer would learn it as one of his/her initial programming assignments. This aspect of programming involves system files. Through file handling, one can perform operations like create, modify, delete etc on system files.


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

Using GCC Compilers, To define the name of the output file that a GCC compiler uses, use the -o The standard way to compile these files, ignoring optimization, debugging, In this example, GCC's C compiler creates the final executable in a file named a.out on Linux calls to memcmp(), memset(), and memcpy() in System V Unix and ISO C​  Set the input character set, used for translation from the characterset of the input file to the source character set used by GCC. If thelocale does not specify, or GCC cannot get this information from thelocale, the default is UTF-8. This can be overridden by either the localeor this command-line option.


Advanced GCC Usage, Right Thing and, at least in our experience, rarely violates the Principle of Least This section describes environment variables that affect how GCC operates. Finally, TMPDIR defines the directory to use for temporary files, which is creates a spec string that says any input file ending with the characters . d should. The rule has no commands. This option does not suppress the preprocessor's debug output, such as-dM. To avoid mixing such debug output with the dependencyrules you should explicitly specify the dependency output file with-MF, or use an environment variable likeDEPENDENCIES_OUTPUT(see Environment Variables).


A Crash Course in C, Pointer Definition and Use; Pointers as Function Arguments: "Call by Value"; Arrays using an editor; compile (and link) it; on Unix systems, use: % gcc program.c A common error when using scanf() is not giving pointers as the arguments; to all functions in the file AFTER the declaration; use extern to make the variable  The rule has nocommands. This option does not suppress the preprocessor’s debug output, such as-dM. To avoid mixing such debug output with the dependencyrules you should explicitly specify the dependency output file with-MF, or use an environment variable likeDEPENDENCIES_OUTPUT(see Environment Variables).


Building native software, The native software plugins make use of the Gradle software model. The native plugins also work with GCC and Clang bundled with Macports. For example, when you apply the c plugin and define a library called Each executable component can produce at least one executable binary (​NativeExecutableBinarySpec). And it is available in GCC for *.c files. 2. There are examples of simple hacks that use macros to give fopen_s, but kind of defeat its purpose being as unsafe as fopen. What I'm asking about, is the way to do it right. Maybe define some GCC-specific variable. Maybe use some MinGW header file. Maybe rebuild some library. There will be other