英文:
C++ program to get the current time
问题
我必须创建一个简单的窗口服务程序,该程序将在Visual Studio中使用C++语言执行。我已经使用了ctime
和localtime
关键字,但出现错误,提示:
> 这个函数或变量可能不安全。考虑使用其他...
我正在使用Visual Studio 2019。
英文:
I have to create a simple window service program which is to be executed in visual studio using C++ language. I have used ctime
and localtime
keywords but it returns an error saying:
> This function or variable may be unsafe. Consider using any other ....
I am using Visual Studio 2019
答案1
得分: 4
你可以使用std::chrono::system_clock::now()
函数来获取当前时间点。
英文:
You can use std::chrono::system_clock::now()
function to get the current time point.
答案2
得分: 3
ctime
和 localtime
是不安全的,因为它们返回指向静态数据的指针。
因此,在VS2019(以及一些较早版本中)提供了 ctime_s
和 localtime_s
,它们需要预先分配的指针来存储返回的值,因此使用起来更安全。
英文:
ctime
and localtime
are unsafe as these returns pointer to static data.
Therefore, ctime_s
and localtime_s
are provided in VS2019 (also in some earlier versions), which takes a pre allocated pointer in which value need to be returned and hence safe to use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论