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