我们有一个在 Visual Studio 2003.NET 中构建的旧项目。仍然工作正常,但我们正在将其迁移到 Visual Studio 2019。令人惊讶的是,到目前为止,问题很少,但有很多警告。一个警告与 fopen
相关:
warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead.
在 VS2019 中将其转换为 fopen_s
很简单,但这不会在 Visual Studio 2003.NET 中构建(因为 fopen_s
不是可识别的关键字)。这可以通过如下代码解决:
#if (_MSC_VER <= 1310)
// Visual Studio 2003.NET or earlier
FILE* fp = fopen(pszTXT, "w");
#elif (_MSC_VER > 1310)
// Newer versions of Visual Studio
FILE* fp = NULL;
fopen_s(&fp, pszTXT, "w");
#endif
fopen_s
何时引入 Visual Studio? MSDN link目前尚不清楚。
这个问题是一般性的,而不是特定于 fopen_s
=> 是否有文档显示引入(或废弃)关键字或方法的 Visual Studio 版本。
最佳答案
来自Visual C++ What's New 2003 through 2015 _s
函数是在 2005 版本中添加的。
<小时/>New CRT Features
- Secure versions of several functions have been added. These functions handle errors in a better way and enforce stricter controls on buffers to help avoid common security flaws. The new secure versions are identified by the _s suffix.
对于 C++ 功能和更改支持,您可以查看 cppreferences C++ compiler support查看哪个编译器版本支持特定的 C++ 功能。这不适用于 _s
函数,因为它们是 C 功能,而不是 C++。
关于c++ - 在 Visual Studio 的哪个版本中引入了函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60490102/