gpt4 book ai didi

c++ - 如何在 C++ 中输出 2013 年 12 月 8 日的日期

转载 作者:行者123 更新时间:2023-11-28 07:14:22 28 4
gpt4 key购买 nike

我收到以下错误消息...

Error 1 error C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS

如有任何帮助,我们将不胜感激。

const char EOL( '\n');  
int main() //draw Xmas tree
{
time_t now = time(0);
tm *ltm = localtime(&now);



cout << "Damon Reynolds Tut 1V"<< ltm->tm_mday << " "
<< 1 + ltm->tm_mon << " " << 1900 + ltm->tm_year;
getValidHeigth(); //call procedure
cout << EOL; //then output a new line
drawBranches(); //call procedure
drawTrunk(); //call procedure

cout << EOL; //then output a new line
system( "PAUSE"); //hold the screen until a key is pressed
return( 0);
}

最佳答案

您正在使用一个已被更安全的函数取代的函数。但是,如果您能够使用 C++11,请考虑使用 std::chrono 库,并使用 C++11 的 put_time 进行格式化:

例子:

#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>

int main()
{
using namespace std::chrono;

auto now = system_clock::to_time_t(system_clock::now());

std::cout << std::put_time(std::localtime(&now), "%d %B %Y")... (your code)
}

您可能需要调整格式才能获得所需的结果。请参阅此处的 put_time 引用:http://en.cppreference.com/w/cpp/io/manip/put_time

关于c++ - 如何在 C++ 中输出 2013 年 12 月 8 日的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20458434/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com