[英]how to remove warning C4996 with gmtime and asctime
I have level 4 warning in my C++ project I want to solve it the warning is 我的C ++项目中有4级警告,我想解决这个警告是
Warning 1 warning C4996: 'gmtime': This function or variable may be unsafe.
警告1警告C4996:'gmtime':该函数或变量可能不安全。 Consider using gmtime_s instead.
考虑改用gmtime_s。 To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。 See online help for details.
详细信息请参见在线帮助。
Warning 2 warning C4996: 'asctime': This function or variable may be unsafe.
警告2警告C4996:'asctime':该函数或变量可能不安全。 Consider using asctime_s instead.
考虑改用asctime_s。 To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。 See online help for details.
详细信息请参见在线帮助。
code C++ 代码C ++
time_t ltime;
time(<ime);
tm* gmt = gmtime(<ime);
char* asctime_remove_nl = asctime(gmt);
Below functions return pointers to static objects that may be overwritten by other subsequent calls(K&R Book). 下面的函数返回指向静态对象的指针,这些指针可能会被其他后续调用(K&R Book)覆盖。 Hence they are not considered to be safe and due to this VS compiler would give the warning/error.
因此,它们不被认为是安全的,并且由于此VS编译器会给出警告/错误。 It can be removed by adding the MACRO in the project(.proj file)(CRT_SECURE_NO_WARNINGS).
可以通过在项目(.proj文件)(CRT_SECURE_NO_WARNINGS)中添加MACRO来删除它。
gmtime()
asctime()
However, we can write the small utility functions which would make the copy of these static strings. 但是,我们可以编写小的实用程序函数来复制这些静态字符串。
// This would return the copy of time/date in std::string object to caller
std::string get_gmtime_asctime() {
time_t ltime;
time(<ime);
struct tm* gt = ::gmtime(<ime);
char* tmp = ::asctime(gt);
std::string output(tmp);
return output;
}
int main() {
std::string out = get_gmtime_asctime();
std::cout<<out<<std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.