目錄
零基礎 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-猿說編程”)]
三.猜你喜歡
- 安裝 Visual Studio
- 安裝 Visual Studio 插件 Visual Assist
- Visual Studio 2008 卸載
- Visual Studio 2003/2015 卸載
- 設置 Visual Studio 字體/背景/行號
- C 語言格式控制符/占位符
- C 語言邏輯運算符
- C 語言三目運算符
- C 語言逗號表達式
- C 語言自加自減運算符(++i / i++)
- C 語言 for 循環
- C 語言 break 和 continue
- C 語言 while 循環
- C 語言 do while 和 while 循環
- C 語言 switch 語句
- C 語言 goto 語句
- C 語言 char 字符串
- C 語言 strlen 函數
- C 語言 sizeof 函數
- C 語言 sizeof 和 strlen 函數區別
- C 語言 strcpy 函數
- C 語言 strcpy_s 函數
- C 語言 memcpy 函數
- C 語言 memcpy_s 函數
- C 語言 error C4996: This function or variable may be unsafe
未經允許不得轉載:猿說編程 » C 語言 error C4996: This function or variable may be unsafe
本文由博客 - 猿說編程 猿說編程 發布!