有没有办法在 GCC 中使用 fopen_s() 或者至少创建一个关于它的#define? [英] Is there a way to use fopen_s() with GCC or at least create a #define about it?

查看:36
本文介绍了有没有办法在 GCC 中使用 fopen_s() 或者至少创建一个关于它的#define?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSVC 编译器说 fopen() 已被弃用,并推荐使用 fopen_s().

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

有什么方法可以使用 fopen_s() 并且仍然可以移植吗?

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

#define 有什么想法吗?

推荐答案

Microsoft 的 *_s 函数不可移植,我通常使用等效的 C89/C99 函数并禁用弃用警告 (#define _CRT_SECURE_NO_DEPRECATE).

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

如果你坚持,你可以使用一个适配器函数(不一定是宏!),在没有 fopen_s() 的平台上委托 fopen(),但是你必须小心映射 errno_t 返回码的值从 errno.

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;
}

但是,我看不出 fopen_s()fopen() 更安全,所以我通常追求可移植性.

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

这篇关于有没有办法在 GCC 中使用 fopen_s() 或者至少创建一个关于它的#define?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆