如何避免与“strcpy()”相关的警告消息? [英] How to avoid an warning message related to "strcpy()"?

查看:128
本文介绍了如何避免与“strcpy()”相关的警告消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 static char BASED_CODE szFilter [] =HTML文件(* .xls)| * .xls |所有文件(*。*)| *。* ||; 

if(AppMarketID == 12)
{
strcpy(szFilter,HTML Files(* .xlsx)| * .xlsx |所有文件(*。*)| * 。* ||);
}
CFileDialog dia(FALSE,,default_name,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);





I警告为 -

警告C4996:'strcpy':此函数或变量可能不安全。请考虑使用strcpy_s。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。有关详细信息,请参阅在线帮助。



您能否指导我如何避免此警告?

谢谢你。

解决方案

只需使用 strcpy_s [ ^ ]以避免警告。



我还建议您使用std :: string或CString来简化操作。

在这种情况下根本没有理由使用strcpy,特别是因为你正在处理静态字符串:

 static char BASED_CODE szFilter1 [] =HTML Files(* .xls )| * .xls |所有文件(*。*)| *。* ||; 
static char BASED_CODE szFilter2 [] =HTML文件(* .xlsx)| * .xlsx |所有文件(*。*)| *。* ||;
CFileDialog dia(FALSE,,default_name,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,(AppMarketID == 12)?szFilter2:szFilter1);


您现在已经发布了三次相同的问题:在哪里 [ ^ ],在哪里 [ ^ ],在这里。您还获得了一些有关如何解决它的有用建议。也许是时候你去了解指针和数组的基础知识,以及它们之间的差异,以及为什么 strcpy()可能是危险的。


static char BASED_CODE szFilter[] = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";
 
    if (AppMarketID == 12)
    { 
      strcpy(szFilter,"HTML Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*||");     
    }
CFileDialog dia(FALSE, "", default_name, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);



I’m getting Warning as -
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Can you please guide me how I can avoid this Warning?
Thank you .

解决方案

Simply use strcpy_s[^] to avoid the warning.

I can also suggest you to use std::string, or CString to simplify things.


There is no reason to use strcpy at all in this case, especially since you are dealing with static strings:

static char BASED_CODE szFilter1[] = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";
static char BASED_CODE szFilter2[] = "HTML Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*||";
CFileDialog dia(FALSE, "", default_name, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, (AppMarketID == 12) ? szFilter2 : szFilter1);


You have now posted this same question three times: here[^], here[^], and here. You have also been given some useful suggestions as to how to resolve it. Maybe it's time you went and read up on the basics of pointers and arrays, and their differences, and why strcpy() can be dangerous.


这篇关于如何避免与“strcpy()”相关的警告消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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