目录

零基础 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)检查”

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CT05UKpd-1628646342860)(https://www.codersrc.com/wp-content/uploads/2021/06/c81e728d9d4c2f6.png “C 语言 error C4996: This function or variable may be unsafe-猿说编程”)]

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,如下图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NMo8zwGU-1628646342862)(https://www.codersrc.com/wp-content/uploads/2021/06/c4ca4238a0b9238.png “C 语言 error C4996: This function or variable may be unsafe-猿说编程”)]

三.猜你喜欢

  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

未经允许不得转载:猿说编程 » C 语言 error C4996: This function or variable may be unsafe

本文由博客 - 猿说编程 猿说编程 发布!

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/shuopython/p/15126911.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!