关于gets()与gets_s()的区别问题

wanghao2979 2007-05-15 10:04:33
是这样
#include <iostream>
using namespace std;
int main()
{
char a[100],n=0;
gets(a);
while(n!=10)
{

n++;
}
system("pause");
return 0;
}
警告:------ 已启动生成: 项目: wanghao, 配置: Debug Win32 ------
正在编译...
cc.cpp
d:\cc\wanghao\wanghao\cc.cpp(6) : warning C4996: “gets”被声明为否决的
d:\microsoft visual studio 8\vc\include\stdio.h(270) : 参见“gets”的声明
消息:“This function or variable may be unsafe. Consider using gets_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.”
正在链接...
正在嵌入清单...
生成日志保存在“file://d:\cc\wanghao\wanghao\Debug\BuildLog.htm”
wanghao - 0 个错误,1 个警告
========== 生成: 1 已成功, 0 已失败, 0 最新, 0 已跳过 ==========
#include <iostream>
using namespace std;
int main()
{
char a[100],n=0;
gets_s(a);
while(n!=10)
{

n++;
}
system("pause");
return 0;
}
没有警告

为什么?
我用的是vs.net 2005
win 32
gets()与gets_s()有什么区别?
...全文
3328 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ygy4444 2011-09-29
  • 打赏
  • 举报
回复
a[3]存放字符串,最后一位a[2]存放的是'\0'
gmlck 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wanghao2979 的回复:]
问题的延伸

#include   <iostream>
using   namespace   std;
int   main()
{
        char   a[3];
gets_s(a);
int   n=0;
while(n!=3)
{
cout < <a[n];
n++;
}
system( "pause ");
return   0;
}

输入1     无错
输入12   无错
输入123   报错

原因??
我定义了a[3]大小是3呀!为什么输入了3个就报错呢?
[/Quote]

a[3]实际上只能存两个字符+"\n"
xiyouboy2008 2008-12-10
  • 打赏
  • 举报
回复
有没有更详细的解释啊??
wanghao2979 2007-05-15
  • 打赏
  • 举报
回复
问题的延伸

#include <iostream>
using namespace std;
int main()
{
char a[3];
gets_s(a);
int n=0;
while(n!=3)
{
cout<<a[n];
n++;
}
system("pause");
return 0;
}

输入1 无错
输入12 无错
输入123 报错

原因??
我定义了a[3]大小是3呀!为什么输入了3个就报错呢?
wanghao2979 2007-05-15
  • 打赏
  • 举报
回复
看了akirya(坏[其实偶不是什么所谓的坏人]) 给我的帮助

我多少明白了一点

简单的说就是一个安全模式

For example

int a[3];
gets_s(a);
cin>>1234;
提示错误
int a[3];
gets(a);
cin>>1234;
不提示错误

学习了....谢谢楼上两位
wanghao2979 2007-05-15
  • 打赏
  • 举报
回复
我去看帮助
jixingzhong 2007-05-15
  • 打赏
  • 举报
回复
一个安全版本,
gets 存在潜在问题 ...

查看一下 MSDN 吧
  • 打赏
  • 举报
回复
gets_s
这个是vs2005提供的安全版本,在 msdn中输入_CRT_SECURE_NO_DEPRECATE这个就能看到了

Significant enhancements have been made to make the CRT more secure. Many CRT functions now have more secure versions. If a new secure function exists, the older, less secure version is marked as deprecated and the new version has the _s ("secure") suffix.

It should be noted that in this context, "deprecated" just means that a function's use is not recommended; it does not indicate that the function is scheduled to be removed from the CRT.

It should also be noted that the secure functions do not prevent or correct security errors; rather, they catch errors when they occur. They perform additional checks for error conditions, and in the case of an error, they invoke an error handler (see Parameter Validation).

For example, the strcpy function has no way of telling if the string that it's copying is too big for its destination buffer. However, its secure counterpart, strcpy_s, takes the size of the buffer as a parameter, so it can determine if a buffer overrun will occur. If you use strcpy_s to copy eleven characters into a ten-character buffer, that is an error on your part; strcpy_s cannot correct your mistake, but it can detect your error and inform you by invoking the invalid parameter handler.

33,322

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧