'本地时间'功能在第二次通话时崩溃

时间:2015-07-27 01:17:31

标签: c localtime

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

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用户,并且在根文件系统上有一个名为/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...");
}

2 个答案:

答案 0 :(得分:2)

已发布(已编辑)的代码未完全编译

建议始终编译并启用所有警告。

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

...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){

强烈建议在事件考虑尝试运行程序之前修复编译问题。

答案 1 :(得分:1)

这会导致未定义的行为:

struct stat *st;
if(stat(route, st) < 0){

您正在将未初始化的变量st传递给库函数,这会导致未定义的行为。

读取stat的{​​{3}}会告诉您,您应该提供指向已存在的缓冲区的指针,stat函数将填写。

例如:

struct stat st = { 0 };     // good practice to zero-initialize
if ( stat(route, &st) < 0 ) {