有没有办法使用fopen_s()与GCC或至少创建一个关于它的#define? [英] Is there a way to use fopen_s() with GCC or at least create a #define about it?
问题描述
MSVC编译器表示不推荐使用 fopen()
,并建议使用 fopen_s()
p>
有什么方法可以使用 fopen_s()
并且仍然是可移植的?
对 #define
?
微软的 * _ s
函数是不可移植的,我通常使用等效的C89 / C99函数并禁用弃用警告( #define _CRT_SECURE_NO_DEPRECATE
) 。
如果你坚持,你可以使用一个代理 fopen()
在没有 fopen_s()
的平台上,但是您必须小心地映射 errno_t
返回代码的值从 errno
。
errno_t fopen_s(FILE ** f,const char * name,const char * mode){
errno_t ret = 0;
assert(f);
* f = fopen(name,mode);
/ *如果(!* f)
ret = errno;无法确定errno和MS'errno_t * /
的一对一映射关系。
return ret;
$ / code>
但是,我没有看到 fopen_s()
比 fopen()
更安全,所以我通常要求可移植性。
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_s()与GCC或至少创建一个关于它的#define?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!