C++ 收到错误“fopen”:此函数或变量可能不安全。编译时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21873048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:51:35  来源:igfitidea点击:

Getting an error "fopen': This function or variable may be unsafe." when compling

c++opencvfopen

提问by SeverusSwan

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.

我是 C++ 新手并打开 CV,因此请帮助我摆脱这个错误。

Thanks

谢谢

void _setDestination(const char* name)
{
    if (name==NULL) {
        stream = stdout;
    }
    else {
        stream = fopen(name,"w");
        if (stream == NULL) {
            stream = stdout;
        }
    }
}

回答by nvoigt

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

这不是错误,而是来自 Microsoft 编译器的警告。

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

选择您的项目并在上下文菜单中单击“属性”。

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

在对话框中,选择Configuration Properties-> C/C++->Preprocessor

In the field PreprocessorDefinitions add ;_CRT_SECURE_NO_WARNINGSto turn those warnings off.

在 PreprocessorDefinitions 字段中添加;_CRT_SECURE_NO_WARNINGS以关闭这些警告。

回答by Blacktempel

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.

或者像微软那样简单地使用 fopen_s 。

But be sure to use the pragma before other headers.

但一定要在其他标头之前使用编译指示。