fopen,_wfopen .error C4996。代码取自MSDN但无法正常工作。 [英] fopen, _wfopen .error C4996. code taken from MSDN but not working.

查看:233
本文介绍了fopen,_wfopen .error C4996。代码取自MSDN但无法正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i从MSDN获取以下代码并粘贴到我的项目中,但它给了我一个错误,上面写着:



错误C4996:'_ wfopen':此函数或变量可能不安全。请考虑使用_wfopen_s。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。



以下是代码:

Hi,

i took the code below from MSDN and pasted in my project, But it gives me an error which says:

error C4996: '_wfopen': This function or variable may be unsafe. Consider using _wfopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

Here is the code:

#include "stdafx.h"//The only line I've added.
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>

#define BUFFER_SIZE 50

int main(int argc, char** argv)
{
wchar_t str[BUFFER_SIZE];
size_t  strSize;
FILE*   fileHandle;

    // Create an the xml file in text and Unicode encoding mode.
    if ((fileHandle = _wfopen( L"_wfopen_test.xml",L"wt+,ccs=UNICODE")) == NULL) // C4996
    // Note: _wfopen is deprecated; consider using _wfopen_s instead
    {
        wprintf(L"_wfopen failed!\n");
        return(0);
    }

    // Write a string into the file.
    wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"<xmlTag>\n");
    strSize = wcslen(str);
    if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
    {
        wprintf(L"fwrite failed!\n");
    }

    // Write a string into the file.
    wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"</xmlTag>");
    strSize = wcslen(str);
    if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
    {
        wprintf(L"fwrite failed!\n");
    }

    // Close the file.
    if (fclose(fileHandle))
    {
        wprintf(L"fclose failed!\n");
    }
    return 0;
}





这是MSDN上的链接:





http://msdn.microsoft.com/en-us/library /yeby3zcb.aspx [ ^ ]



为什么会出现这个错误?

我该如何解决?



Here is the link on MSDN:


http://msdn.microsoft.com/en-us/library/yeby3zcb.aspx[^]

Why does this error come up?
How should i fix it?

推荐答案

这是微软的事情,它们可能不安全:你的用法看起来很好。

您可以(按照建议)禁用编译器弃用或使用 _wfopen_s
It's a Microsoft thing, they are potentially unsafe: your usage looks perfectly fine.
You may either (as suggested) disable the compiler deprecation or use _wfopen_s.


这篇关于fopen,_wfopen .error C4996。代码取自MSDN但无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆