求教高手,解释为什么出现这种警告,该怎么消除?

chaojiew 2008-05-27 11:12:27

我写这个:timeinfo=localtime( &rawtime );总是出现这样的警告,

This function or variable may be unsafe.
Consider using localtime_s instead. To disable deprecation,
use _CRT_SECURE_NO_WARNINGS. See online help for details.

当我改成 timeinfo=localtime_s( &rawtime ); 时,又出现
error C2660: “localtime_s”: 函数不接受 1 个参数,,
---------------------------

我查了查msdn,
errno_t localtime_s(
struct tm* _tm,
const time_t *time
);
Parameters

_tm
Pointer to the time structure to be filled in.

time
Pointer to the stored time.
---------------
把代码改成
localtime_s( timeinfo,&rawtime );后,又有警告说
timeinfo未初始化。。。

晕了。。这不是一个个别的现象,我在用c里面其它的函数时,也出过这种警告,加了
_s后,也是说参数不匹配,,例如strcpy


求教高手,解释为什么出现这种警告,该怎么消除?
...全文
1190 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxc520xx 2010-01-13
  • 打赏
  • 举报
回复
楼主 我的也是这个问题 到现在都不知道怎么解决 烦人得很。。。。
netlcx 2008-05-29
  • 打赏
  • 举报
回复
去掉警告 在项目属性里面——配置属性——C/C++——命令行 中输入 /D "_CRT_SECURE_NO_WARNINGS"

好像是这样的
palmax 2008-05-29
  • 打赏
  • 举报
回复
errno_t _localtime_s(
struct tm* _tm,
const time_t *time
);

_tm
Pointer to the time structure to be filled in.


MSDN上说的很清楚了, _tm 参数是 “to be filled in”, 也就是说是个输出参数
timeinfo = {0}; 是把结构中各成员初始化为0

而且MSDN在函数下面还有例子就是这样用: 我编译了也没问题,偏偏到你机器上就有异常了?

(摘自MSDN)

// crt_localtime_s.c
/* This program uses _time64 to get the current time
* and then uses _localtime64_s() to convert this time to a structure
* representing the local time. The program converts the result
* from a 24-hour clock to a 12-hour clock and determines the
* proper extension (AM or PM).
*/

#include <stdio.h>
#include <string.h>
#include <time.h>

int main( void )
{
struct tm newtime;
char am_pm[] = "AM";
__time64_t long_time;
char timebuf[26];
errno_t err;

// Get time as 64-bit integer.
_time64( &long_time );
// Convert to local time.
err = _localtime64_s( &newtime, &long_time );
if (err)
{
printf("Invalid argument to _localtime64_s.");
exit(1);
}
if( newtime.tm_hour > 12 ) // Set up extension.
strcpy_s( am_pm, sizeof(am_pm), "PM" );
if( newtime.tm_hour > 12 ) // Convert from 24-hour
newtime.tm_hour -= 12; // to 12-hour clock.
if( newtime.tm_hour == 0 ) // Set hour to 12 if midnight.
newtime.tm_hour = 12;

// Convert to an ASCII representation.
err = asctime_s(timebuf, 26, &newtime);
if (err)
{
printf("Invalid argument to asctime_s.");
exit(1);
}
printf( "%.19s %s\n", timebuf, am_pm );
}
chaojiew 2008-05-29
  • 打赏
  • 举报
回复
运行出现异常
chaojiew 2008-05-29
  • 打赏
  • 举报
回复
这样根本没有对timeinfo赋值!!!
localtime_s( timeinfo,&rawtime );
chao_83 2008-05-28
  • 打赏
  • 举报
回复
编译器已经告诉你了 : use _CRT_SECURE_NO_WARNINGS
这样就行了吧:
#define _CRT_SECURE_NO_WARNINGS
palmax 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 palmax 的回复:]
编译器是你的朋友,而不是你的敌人
它提出的警告你应该消除,而不是用预编译指令去disable。
vs2005执行了ISO标准

tm timeinfo = {0};
localtime_s( &timeinfo,&rawtime );
[/Quote]

tm timeinfo = {0}; 就是初始化
palmax 2008-05-28
  • 打赏
  • 举报
回复
编译器是你的朋友,而不是你的敌人
它提出的警告你应该消除,而不是用预编译指令去disable。
vs2005执行了ISO标准

tm timeinfo = {0};
localtime_s( &timeinfo,&rawtime );
dwen20 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chaojiew 的回复:]

这样的话,提示timeinfo未被初始化啊
[/Quote]
那就初试化啊,传入的是地址,当然得初始化。
chaojiew 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 palmax 的回复:]
编译器是你的朋友,而不是你的敌人
它提出的警告你应该消除,而不是用预编译指令去disable。
vs2005执行了ISO标准

tm timeinfo = {0};
localtime_s( &timeinfo,&rawtime );
[/Quote]
这样的话,提示timeinfo未被初始化啊
dbger 2008-05-27
  • 打赏
  • 举报
回复
友情提示
------------
请及时结贴
dbger 2008-05-27
  • 打赏
  • 举报
回复
LZ用的是VS2005吧

只是VS2005的检查更严格了
可以使用它提供的安全函数,就是_s的

如果想使用原来的函数又想去掉警告,可以修改下工程设置
或者:#pragma warning(disable:4996)

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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