gpt4 book ai didi

c - 'localtime' 函数在第二次调用时崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 07:07:14 28 4
gpt4 key购买 nike

所以,我写了下一个函数:

void actualizetime(){
puts("actualizetime called"); //debugging purposes.
time_t rawtime;
puts("static time_t defined"); //debugging purposes.
struct tm *timeinfo;
puts("struct tm * defined"); //debugging purposes.
time(&rawtime);
puts("time function called"); //debugging purposes.
timeinfo = localtime(&rawtime);
puts("timeinfo has localtime"); //debugging purposes.

actualtime.year = timeinfo->tm_year + 1900;
actualtime.month = timeinfo->tm_mon;
actualtime.day = timeinfo->tm_mday;
actualtime.hour = timeinfo->tm_hour;
actualtime.min = timeinfo->tm_min;
actualtime.sec = timeinfo->tm_sec;
}

事实是,这个功能只用了一次。第二次调用该函数时,timeinfo = localtime(&rawtime); 崩溃了。出了什么问题?以及,我该如何解决?

我不认为 actualtime 结构很重要,但是,无论如何:

struct {
int year;
int month;
int day;

int hour;
int min;
int sec;
} actualtime;

编辑:

完整程序(仅限linux):

警告:要运行此程序,您必须是 root 用户,并且在 root 文件系统上有一个名为 /pragma/regfiles/

#include<sys/stat.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>

void actualizetime(void);
void openregfile(void);
void closeregfile(void);
void addreg(char *,char *);
void terminate(void);

char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

FILE *regfile;

struct {
int year;
int month;
int day;

int hour;
int min;
int sec;
} actualtime;

void actualizetime(){
puts("-actualizetime called"); //debugging purposes.
time_t rawtime;
puts("-static time_t defined"); //debugging purposes.
struct tm *timeinfo;
puts("-struct tm * defined"); //debugging purposes.
time(&rawtime);
puts("-time function called"); //debugging purposes.
timeinfo = localtime(&rawtime);
puts("-timeinfo has localtime"); //debugging purposes.

actualtime.year = timeinfo->tm_year + 1900;
actualtime.month = timeinfo->tm_mon;
actualtime.day = timeinfo->tm_mday;
actualtime.hour = timeinfo->tm_hour;
actualtime.min = timeinfo->tm_min;
actualtime.sec = timeinfo->tm_sec;
}

void openregfile(void){
char route[64];
struct stat *st;

if(!route){
puts("Error initializing files");
exit(1);
}

actualizetime();

sprintf(route, "/pragma/regfiles/%04d/", actualtime.year);
if(stat(route, st) < 0){
if(mkdir(route, 0600) < 0){
puts("ERROR: cannot create directory for regs files! (1)");
exit(1);
}
}

sprintf(route, "/pragma/regfiles/%04d/%s", actualtime.year, months[actualtime.month]);
if(stat(route, st) < 0){
if(mkdir(route, 0600) < 0){
puts("ERROR: cannot create directory for regs files! (2)");
exit(1);
}
}

sprintf(route, "/pragma/regfiles/%04d/%s/%02d.reg", actualtime.year, months[actualtime.month], actualtime.day);
if((regfile = fopen(route, "a")) == NULL){
puts("ERROR: cannot create reg file!");
exit(1);
}
addreg("log", "reg start");
}

void closeregfile(){
addreg("log", "reg stop");
fclose(regfile);
}

void addreg(char *label,char *msg){
puts("-before actualize time"); //debugging purposes.
actualizetime();
puts("-after actualize time"); //debugging purposes.
printf("%d%d%d", actualtime.hour, actualtime.min, actualtime.sec);
fprintf(regfile, "[%s]:%04d%s%02d_%02d%02d%02d %s\n", label, actualtime.year, months[actualtime.month], actualtime.day, actualtime.hour, actualtime.min, actualtime.sec, msg);
fflush(regfile);
}






int main(void){
openregfile();

//Do something

terminate();
return 0;
}

void terminate(void){
closeregfile();
puts("Terminating...");
}

最佳答案

发布(编辑)的代码没有干净地编译

建议始终在启用所有警告的情况下进行编译。

然后您会看到以下两条消息:

...c:49:6 warning: the addres of 'route' will always be evaluated as 'true' [-Waddress]

if(!route){

...c:57:10: warning 'st' is used unitialized in this function [-Wuninitalized]

if(stat(route, st) < 0){

强烈建议在考虑尝试运行程序之前解决编译问题。

关于c - 'localtime' 函数在第二次调用时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31643701/

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