| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3895 人关注过本帖
标题:排除 vs2005 中的不安全函数警告
只看楼主 加入收藏
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
结帖率:79.17%
收藏
 问题点数:0 回复次数:6 
排除 vs2005 中的不安全函数警告
下面的代码:
#include <stdio.h>
#include <minmax.h>

int main( )
{
    int a,b,c;
    scanf("%d,%d",&a,&b);
    c=max(a,b);
    printf("max=%d",c);
    return 0;
}
使用vs2005编译时会遇到这样一个warning: warning C4996: 'scanf' was declared deprecated
其实 warning C4996的详细含义就是:'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.翻译过来,就是scanf的声明在VS2005中被认为是不安全的,让你使用scanf_S来代替。
知道了原因,那解决就方便了,只要在#include <stdio.h>前面添加
#define _CRT_SECURE_NO_DEPRECATE 或者 scanf函数修改为scanf_s即可。具体如下:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <minmax.h>

int main( )
{
    int a,b,c;
    scanf("%d,%d",&a,&b);
    c=max(a,b);
    printf("max=%d",c);
    return 0;
}
或者
#include <stdio.h>
#include <minmax.h>

int main( )
{
    int a,b,c;
    scanf_s("%d,%d",&a,&b);
    c=max(a,b);
    printf("max=%d",c);
    return 0;
}
搜索更多相关主题的帖子: 安全 scanf 警告 scanf_s warning 
2008-08-13 20:07
爱喝牛奶的猫咪
Rank: 1
来 自:QQ群46520219
等 级:禁止访问
帖 子:513
专家分:0
注 册:2008-6-16
收藏
得分:0 
Orz。。。。。这也算精华。。。。。


[color=white]<" border="0" />>
2008-08-14 19:20
zhaoyg
Rank: 1
等 级:新手上路
帖 子:328
专家分:0
注 册:2006-8-28
收藏
得分:0 
谢了

麻雀飞上枝头变凤凰,而菜鸟的我飞上枝头却感冒了,为什么我的脑袋如此的不管用呢。
2008-08-14 19:20
恶魔岛救公主
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2008-08-14 21:49
lingluoz
Rank: 2
来 自:苏州科技学院
等 级:新手上路
威 望:4
帖 子:749
专家分:0
注 册:2008-2-2
收藏
得分:0 
太感谢了。。每次用VC2008编译程序都爆这个 WARNING。。。看着就不爽

Murphy's Law :
If there are two or more ways to do something, and one of those ways can result in a catastrophe, then someone will do it.
2008-08-14 22:13
蓝色神话
Rank: 2
等 级:论坛游民
威 望:1
帖 子:404
专家分:24
注 册:2006-5-11
收藏
得分:0 
没什么用!
2008-08-15 13:14
vfdff
Rank: 6Rank: 6
等 级:侠之大者
威 望:8
帖 子:2172
专家分:425
注 册:2005-7-15
收藏
得分:0 
fopen_s替代fopen的方法
// crt_fopen_s.c
// This program opens two files. It uses
// fclose to close the first file and
// _fcloseall to close all remaining files.
 
#include <stdio.h>

FILE *stream, *stream2;

int main( void )
{
   int numclosed;
   errno_t err;

   // Open for read (will fail if file "crt_fopen_s.c" does not exist)
   if( (err  = fopen_s( &stream, "crt_fopen_s.c", "r" )) !=0 )
      printf( "The file 'crt_fopen_s.c' was not opened\n" );
   else
      printf( "The file 'crt_fopen_s.c' was opened\n" );

   // Open for write
   if( (err = fopen_s( &stream2, "data2", "w+" )) != 0 )
      printf( "The file 'data2' was not opened\n" );
   else
      printf( "The file 'data2' was opened\n" );

   // Close stream if it is not NULL
   if( stream)
   {
      if ( fclose( stream ) )
      {
         printf( "The file 'crt_fopen_s.c' was not closed\n" );
      }
   }

   // All other files are closed:
   numclosed = _fcloseall( );
   printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}
当然也可以使用 #define _CRT_SECURE_NO_DEPRECATE 屏蔽这个C4996警告,但是本身没有确保代码安全
2008-08-15 15:22
快速回复:排除 vs2005 中的不安全函数警告
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020113 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved