我试图阻止重建轮子.我正在解析一个时间作为其中一个值的输入文件.我需要一个数据结构来保存输入文件的所有值,而不是为时间字段创建自定义结构,我只想使用来自ctime的struct tm.
我遇到了一个奇怪的错误,所以希望你们中的一个可以帮助我.这是我的测试代码(用于我的概念证明):
#include <ctime>
#include <cstdio>
int main()
{
int Oldhour = 16;
int OldSecond = 25;
int OldMinute = 20;
time_t seconds;
struct tm * timeinfo;
timeinfo->tm_hour = Oldhour;
timeinfo->tm_min = OldMinute;
timeinfo->tm_sec = OldSecond;
int hour, min, sec;
hour = timeinfo->tm_hour;
min = timeinfo->tm_min;
sec = timeinfo->tm_sec;
printf("%d:%d:%d", hour, min, sec);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这编译得很好,它完全符合我的要求并打印"16:20:25",因此它以我想要的方式存储信息.但是,如果我删除行"time_t seconds;" 它立刻崩溃了.