小编And*_*rew的帖子

除非首先声明time_t,否则程序在使用struct tm时会崩溃

我试图阻止重建轮子.我正在解析一个时间作为其中一个值的输入文件.我需要一个数据结构来保存输入文件的所有值,而不是为时间字段创建自定义结构,我只想使用来自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;" 它立刻崩溃了.

c c++

0
推荐指数
1
解决办法
903
查看次数

标签 统计

c ×1

c++ ×1