<p>With the <em>Visual Studio 2005 C++ compiler</em>, I get the following warning when my code uses the <code>fopen()</code> and such calls:</p>
<pre class="lang-none prettyprint-override"><code>1&gt;foo.cpp(5) : warning C4996: 'fopen' was declared deprecated
1&gt;        c:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
1&gt;        Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
</code></pre>
<p>How do I prevent this?</p>

[{'answer_id': 14387, 'author': 'Ashwin Nanjappa', 'author_id': 1630, 'author_profile': 'https://Stackoverflow.com/users/1630', 'pm_score': 8, 'selected': True, 'text': "<p>It looks like Microsoft has deprecated lots of calls which use buffers to improve code security. However, the solutions they're providing aren't portable. Anyway, if you aren't interested in using the secure version of their calls (like <strong>fopen_s</strong>), you need to place a definition of <strong>_CRT_SECURE_NO_DEPRECATE</strong> before your included header files. For example:</p>\n\n<pre><code>#define _CRT_SECURE_NO_DEPRECATE\n#include &lt;stdio.h&gt;\n</code></pre>\n\n<p>The preprocessor directive can also be added to your project settings to effect it on all the files under the project. To do this add <strong>_CRT_SECURE_NO_DEPRECATE</strong> to <em>Project Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions</em>.</p>\n"}, {'answer_id': 14506, 'author': 'John Sibly', 'author_id': 1078, 'author_profile': 'https://Stackoverflow.com/users/1078', 'pm_score': 5, 'selected': False, 'text': "<p>Well you could add a:</p>\n\n<pre><code>#pragma warning (disable : 4996)\n</code></pre>\n\n<p>before you use fopen, but have you considered using fopen_s as the warning suggests? It returns an error code allowing you to check the result of the function call. </p>\n\n<p>The problem with just disabling deprecated function warnings is that Microsoft may remove the function in question in a later version of the CRT, breaking your code (as stated below in the comments, this won't happen in this instance with fopen because it's part of the C &amp; C++ ISO standards).</p>\n"}, {'answer_id': 91698, 'author': 'Joseph Holsten', 'author_id': 16981, 'author_profile': 'https://Stackoverflow.com/users/16981', 'pm_score': 2, 'selected': False, 'text': '<p>Consider using a portability library like <a href="http://www.gtk.org/" rel="nofollow noreferrer">glib</a> or the <a href="http://apr.apache.org/" rel="nofollow noreferrer">apache portable runtime</a>. These usually provide safe, portable alternatives to calls like these. It\'s a good thing too, because these insecure calls are deprecated in most modern environments.</p>\n'}, {'answer_id': 284698, 'author': 'tragomaskhalos', 'author_id': 31140, 'author_profile': 'https://Stackoverflow.com/users/31140', 'pm_score': 4, 'selected': False, 'text': '<p>This is just Microsoft being cheeky. "Deprecated" implies a language feature that may not be provided in future versions of the standard language / standard libraries, as decreed by the standards committee. It does not, or should not mean, "we, unilaterally, don\'t think you should use it", no matter how well-founded that advice is.</p>\n'}, {'answer_id': 8069439, 'author': 'Denys Yurchenko', 'author_id': 1038233, 'author_profile': 'https://Stackoverflow.com/users/1038233', 'pm_score': 3, 'selected': False, 'text': '<p>If you code is intended for a different OS (like Mac&nbsp;OS&nbsp;X, Linux) you may use following:</p>\n\n<pre><code>#ifdef _WIN32\n#define _CRT_SECURE_NO_DEPRECATE\n#endif\n</code></pre>\n'}, {'answer_id': 27709586, 'author': 'Karthik_elan', 'author_id': 3220295, 'author_profile': 'https://Stackoverflow.com/users/3220295', 'pm_score': 0, 'selected': False, 'text': "<p>I also got the same problem. When I try to add the opencv library</p>\n\n<pre><code>#include &lt;opencv\\cv.h&gt;\n</code></pre>\n\n<p>I got not a warning but an error.</p>\n\n<pre><code>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.    c:\\program files (x86)\\opencv\\build\\include\\opencv2\\flann\\logger.h  \n</code></pre>\n\n<p>I also used the preprocessor directives as mentioned. But that didn't solve the problem.</p>\n\n<p>I solved it by doing as follows:</p>\n\n<ul>\n<li><strong>Go to Properties -> C/C++ -> Precompiled Headers -> Choose Not Using Precompiled Headers in Precompiled Header.</strong></li>\n</ul>\n"}, {'answer_id': 28692707, 'author': 'JTIM', 'author_id': 2076775, 'author_profile': 'https://Stackoverflow.com/users/2076775', 'pm_score': 1, 'selected': False, 'text': '<p>If you want it to be used on many platforms, you could as commented use defines like:</p>\n\n<pre><code>#if defined(_MSC_VER) || defined(WIN32)  || defined(_WIN32) || defined(__WIN32__) \\\n                        || defined(WIN64)    || defined(_WIN64) || defined(__WIN64__) \n\n        errno_t err = fopen_s(&amp;stream,name, "w");\n\n#endif\n\n#if defined(unix)        || defined(__unix)      || defined(__unix__) \\\n                        || defined(linux)       || defined(__linux)     || defined(__linux__) \\\n                        || defined(sun)         || defined(__sun) \\\n                        || defined(BSD)         || defined(__OpenBSD__) || defined(__NetBSD__) \\\n                        || defined(__FreeBSD__) || defined __DragonFly__ \\\n                        || defined(sgi)         || defined(__sgi) \\\n                        || defined(__MACOSX__)  || defined(__APPLE__) \\\n                        || defined(__CYGWIN__) \n\n        stream = fopen(name, "w");\n\n#endif\n</code></pre>\n'}, {'answer_id': 35191511, 'author': 'riderBill', 'author_id': 4079867, 'author_profile': 'https://Stackoverflow.com/users/4079867', 'pm_score': 1, 'selected': False, 'text': '<p>Many of Microsoft\'s secure functions, including fopen_s(), are part of C11, so they should be portable now. You should realize that the secure functions differ in exception behaviors and sometimes in return values. Additionally you need to be aware that while these functions are standardized, it\'s an <em>optional</em> part of the standard (Annex K) that at least glibc (default on Linux) and FreeBSD\'s libc don\'t implement.</p>\n\n<p>However, I fought this problem for a few years. I posted a larger set of conversion macros <a href="https://stackoverflow.com/questions/6234682/need-a-define-for-visual-studio-versions-that-include-secure-string-functions/35191283#35191283">here.</a>, For your immediate problem, put the following code in an include file, and include it in your source code:</p>\n\n<pre><code>#pragma once\n#if !defined(FCN_S_MACROS_H)\n   #define   FCN_S_MACROS_H\n\n   #include &lt;cstdio&gt;\n   #include &lt;string&gt; // Need this for _stricmp\n   using namespace std;\n\n   // _MSC_VER = 1400 is MSVC 2005. _MSC_VER = 1600 (MSVC 2010) was the current\n   // value when I wrote (some of) these macros.\n\n   #if (defined(_MSC_VER) &amp;&amp; (_MSC_VER &gt;= 1400) )\n\n      inline extern\n      FILE*   fcnSMacro_fopen_s(char *fname, char *mode)\n      {  FILE *fptr;\n         fopen_s(&amp;fptr, fname, mode);\n         return fptr;\n      }\n      #define fopen(fname, mode)            fcnSMacro_fopen_s((fname), (mode))\n\n   #else\n      #define fopen_s(fp, fmt, mode)        *(fp)=fopen( (fmt), (mode))\n\n   #endif //_MSC_VER\n\n#endif // FCN_S_MACROS_H\n</code></pre>\n\n<p>Of course this approach does not implement the expected exception behavior.</p>\n'}, {'answer_id': 42908405, 'author': 'Marcelo Coronel', 'author_id': 7642855, 'author_profile': 'https://Stackoverflow.com/users/7642855', 'pm_score': 1, 'selected': False, 'text': '<p>For those who are using Visual Studio 2017 version, it seems like the preprocessor definition required to run unsafe operations has changed. Use instead:</p>\n\n<pre><code>#define _CRT_SECURE_NO_WARNINGS\n</code></pre>\n\n<p>It will compile then.</p>\n'}, {'answer_id': 45114212, 'author': 'Bryant', 'author_id': 7666772, 'author_profile': 'https://Stackoverflow.com/users/7666772', 'pm_score': 3, 'selected': False, 'text': '<p>I\'am using VisualStdio 2008.\nIn this case I often set <strong><code>Preprocessor Definitions</code></strong></p>\n\n<blockquote>\n  <p><strong>Menu \\ Project \\ [ProjectName] Properties... Alt+F7</strong></p>\n</blockquote>\n\n<p>If click this menu or press Alt + F7 in project window, you can see <strong>"Property Pages"</strong> window.</p>\n\n<p>Then see menu on left of window.</p>\n\n<blockquote>\n  <p><strong>Configuration Properties \\ C/C++ \\ Preprocessor</strong></p>\n</blockquote>\n\n<p>Then add <strong><code>_CRT_SECURE_NO_WARNINGS</code></strong> to <strong>\\ Preprocessor Definitions</strong>.</p>\n'}]
['https://Stackoverflow.com/questions/14386', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1630/']