35

I'm receiving this error when compiling:

'fopen': This function or variable may be unsafe. 
Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

I'm new to C++ and open CV, therefore please help me to get rid of this error.

Thanks

void _setDestination(const char* name)
{
    if (name==NULL) {
        stream = stdout;
    }
    else {
        stream = fopen(name,"w");
        if (stream == NULL) {
            stream = stdout;
        }
    }
}
Donald_W
  • 1,773
  • 21
  • 35
SeverusSwan
  • 361
  • 1
  • 3
  • 4

2 Answers2

98

This is not an error, it is a warning from your Microsoft compiler.

Select your project and click "Properties" in the context menu.

In the dialog, chose Configuration Properties -> C/C++ -> Preprocessor

In the field PreprocessorDefinitions add ;_CRT_SECURE_NO_WARNINGS to turn those warnings off.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 4
    I would not disable this warning. Instead of this I would use more secure version of fopen, it means fopen_s. See ['Security Features in the CRT'](http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx) for details. – Bogdan Feb 19 '14 at 06:53
  • 10
    @Bogdan Yes, that's nice if you are Microsoft only, but this is an MS extension and not standard C++. If this is a library or part of a project that is not 100% Microsoft only, you should not use the _s extensions but instead rely on writing good standard conforming code instead. – nvoigt Feb 19 '14 at 06:57
  • you can still use fopen_s. just put inside conditional compilation block for win32. such blocks are unavoidable if you really are working on cross platform projects – Zaw Lin Feb 19 '14 at 07:02
  • 4
    I don't see why one would write an extra conditional block, to circumvent standard conforming behaviour. Condition blocks are for things that change from platform to platform and are not in the standard. This is not. This is standard C and C++. – nvoigt Feb 19 '14 at 07:05
  • 2
    op has not specified standard conforming behaviour as requirement. i simply provided an option to fix the mentioned error. – Zaw Lin Feb 19 '14 at 07:08
  • 3
    @nvoigt With Visual Studio 2013 it is indeed generating a compilation error with the default project configuration, not a warning any more like until Visual Studio 2012. – Étienne Jun 09 '14 at 16:56
  • Disabeling critical warning flags _CRT_SECURE_NO_WARNINGS is BAD advise and bad programming practice – user3548161 Jun 14 '21 at 08:15
  • @user3548161 At the time of writing it was the only way of writing standard cornforming code. Times have changed though, if you want to add an updated answer, feel free to do so. – nvoigt Jun 14 '21 at 08:49
  • 1
    Doesn't work on Visual Studio 2019. – Patapoom Mar 29 '22 at 18:37
25

This is a warning for usual. You can either disable it by

#pragma warning(disable:4996)

or simply use fopen_s like Microsoft has intended.

But be sure to use the pragma before other headers.

Blacktempel
  • 3,935
  • 3
  • 29
  • 53