C语言 error C4996: This function or variable may be unsafe

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门


一.error C4996 简介

error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.  

       正常调用 fopen / memcpy / strcpy 等函数报错 error C4996,是因为许多函数、 成员函数,模板函数和 Visual Studio 中的库中的全局变量标记为弃       用 这些函数被弃用,因为它们可能具有不同的首选的名称,可能不安全或具有更加安全的变体,或可能已过时。 许多弃用消息包括不推荐使用的函数或全局变量的建议的替换。


二.error C4996 解决办法

1.采用_s结尾的安全版本

       将上面的 fopen 函数改为 fopen_s 函数,例如:

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 error C4996:  This function or variable may be unsafe
//@Time:2021/06/03 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/    

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "windows.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    //FILE* fp = fopen("d:/12345633.txt", "r"); //error c4996
    FILE* fp = NULL;
    fopen_s(&fp, "d:/12345633.txt", "r"); // ok版本
    if (fp)
    {
        printf("打开文件成功  \n");
        fclose(fp);
    }
    else
        printf("打开文件失败,失败错误号:%d  \n",GetLastError());
    system("pause");
    return 0;
}

2.去掉 visual studio “安全开发生命周期(SDL)检查”

C语言 error C4996:  This function or variable may be unsafe 插图1

3.#pragma warning( disable : 4996)

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:C语言教程 - C语言 error C4996:  This function or variable may be unsafe
//@Time:2021/06/03 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/    

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "windows.h"
using namespace std;
#pragma warning( disable : 4996)
int _tmain(int argc, _TCHAR* argv[])
{
    FILE* fp = fopen("d:/12345633.txt", "r");
    if (fp)
    {
        printf("打开文件成功  \n");
        fclose(fp);
    }
    else
        printf("打开文件失败,失败错误号:%d  \n",GetLastError());
    system("pause");
    return 0;
}

4._CRT_SECURE_NO_WARNINGS

       项目 =》属性 =》c/c++ =》预处理器=》点击预处理器定义,编辑,加入_CRT_SECURE_NO_WARNINGS,如下图:

C语言 error C4996:  This function or variable may be unsafe 插图1

三.猜你喜欢

  1. 安装 Visual Studio
  2. 安装 Visual Studio 插件 Visual Assist
  3. Visual Studio 2008 卸载
  4. Visual Studio 2003/2015 卸载
  5. 设置 Visual Studio 字体/背景/行号
  6. C语言格式控制符/占位符
  7. C语言逻辑运算符
  8. C语言三目运算符
  9. C语言逗号表达式
  10. C语言自加自减运算符(++i / i++)
  11. C语言 for 循环
  12. C语言 break 和 continue
  13. C语言 while 循环
  14. C语言 do while 和 while 循环
  15. C语言 switch 语句
  16. C语言 goto 语句
  17. C语言 char 字符串
  18. C语言 strlen 函数
  19. C语言 sizeof 函数
  20. C语言 sizeof 和 strlen 函数区别
  21. C语言 strcpy 函数
  22. C语言 strcpy_s 函数
  23. C语言 memcpy 函数
  24. C语言 memcpy_s 函数
  25. C语言 error C4996: This function or variable may be unsafe

ChatGPT 3.5 国内中文镜像站免费使用啦
文章版权声明 1、本网站名称:猿说编程
2、本站永久网址:https://www.codersrc.com
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。

© 版权声明
THE END
喜欢就支持一下吧
点赞3 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容