有没有办法将 fopen_s() 与 GCC 一起使用,或者至少创建一个关于它的 #define ?
MSVC 编译器表示 fopen()
已弃用,并建议使用 fopen_s()
。
有什么方法可以使用 fopen_s()
并且仍然可移植吗?
对于 #define
有什么想法吗?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Microsoft 的 *_s 函数不可移植,我通常使用等效的 C89/C99 函数并禁用弃用警告 (
#define _CRT_SECURE_NO_DEPRECATE
)。如果您坚持,您可以使用适配器函数(不一定是宏!)在没有
fopen_s()
的平台上委托fopen()
,但您必须请小心从errno
映射errno_t
返回代码的值。但是,我不明白
fopen_s()
比fopen()
更安全,所以我通常会考虑可移植性。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 havefopen_s()
, but you must be careful to map values oferrno_t
return code fromerrno
.However, I fail to see how
fopen_s()
is any more secure thanfopen()
, so I usually go for portability.在 C/C++ 代码中,
在 Makefile 中
请注意,成功时 fopen_s 返回 0,而 fopen 返回非零文件指针。因此需要在宏末尾添加“==NULL”,例如:
In C/C++ code,
In Makefile
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.:
如果您使用 C11,
fopen_s
是一个标准库。在
gcc
中,您需要使用--std=c11
参数。if you are using C11,
fopen_s
is a standard library.In
gcc
you need to use--std=c11
parameter.微软的许多安全功能都包含在C11标准的Annex K中,但并未得到广泛支持,因此可移植性仍然是一个问题。某些应用程序需要提高安全性;也许将来的支持会有所改善。
我过去是这样做的:
这个宏简单直接,对于快速而肮脏的事情来说足够好了,但是它不提供 fopen_s 的异常行为,并且它不会提供真正的 fopen_s 的安全性功能。
@Alex B 的上述函数方法部分重现了失败时的正确行为;他返回 errno (= EINVAL)。他的方法可以通过生成无效参数异常来进一步扩展,以更全面地重现 fopen_s 的行为。
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:
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.
根据 https://en.cppreference.com/w/c/io/fopen< /a>
可以在标准库上启用 *_s 函数:
Accroding to https://en.cppreference.com/w/c/io/fopen
it's possible to enable *_s functions on the standard library: