【发布时间】:2015-02-25 18:03:47
【问题描述】:
我正在尝试创建一个时间戳,目前我正在尝试将其输出到屏幕上。
我尝试使用的函数是 localtime_s。
但是我无法让它工作,也找不到任何关于如何使用它的工作示例。
我遇到以下错误:
- 错误 1 错误 C2660: 'localtime_s' : 函数不接受 1 个参数
- “errno_t”类型的值不能分配给“tm *”类型的实体
- “time_t *”类型的参数与“tm *”类型的参数不兼容
-
函数调用中的参数太少
time_t rawtime; struct tm * timeinfo; time(&rawtime); timeinfo = localtime_s(&rawtime); asctime(timeinfo); cout << "Current local time and date: " << timeinfo << endl;
谁能给点建议?
编辑
使用解决 cout 问题的答案,Visual Studio 不会让我运行 asctime。所以我尝试使用 asctime_s 然后显示 “没有重载函数“asctime_s”的实例与参数列表匹配,参数类型为:(tm *, time_t *)”
但是,使用指向 timeinfo 和 rawtime 的指针会导致更多错误。
现在的代码是:
time_t rawtime;
tm timeinfo;
errno_t result = localtime_s(&timeinfo, &rawtime);
cout << "Current local time and date: " << asctime_s(&timeinfo, &rawtime) << endl;
亲切的问候
【问题讨论】:
-
注意错误信息,他们很清楚问题是什么。另请阅读 documentation 以了解您遇到问题的 API 调用,这也将有助于解决问题。
标签: c++