Reputation:
I have level 4 warning in my C++ project I want to solve it the warning is
Warning 1 warning C4996: 'gmtime': This function or variable may be unsafe. Consider using gmtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Warning 2 warning C4996: 'asctime': This function or variable may be unsafe. Consider using asctime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
code C++
time_t ltime;
time(<ime);
tm* gmt = gmtime(<ime);
char* asctime_remove_nl = asctime(gmt);
Upvotes: 0
Views: 5292
Reputation: 5731
Below functions return pointers to static objects that may be overwritten by other subsequent calls(K&R Book). Hence they are not considered to be safe and due to this VS compiler would give the warning/error. It can be removed by adding the MACRO in the project(.proj file)(CRT_SECURE_NO_WARNINGS).
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;
}
Upvotes: 1