0

我以前遇到过这样的事情,但通常只是在处理stdout.

#define LOG 1

static void logoutput(char *info) {
    FILE *dbg;
    dbg = fopen(LOGFILE, "a+");
    if(dbg != NULL) {
        fprintf(dbg, "Mix%d: %s\n", MIXNUM, info);
        fclose(dbg);
    }
    else printf("Error writing to log file.\n");
    return;
}

if (data[0] != NULL) {
    if(LOG) logoutput("WARNING, something bad happened on port 1!");
    exit(1); //Adding a sleep(1); before this didn't help.
}

我遇到的问题是,当 时data[x] != NULL,程序立即退出并且从不记录任何内容。

我尝试添加一个sleep(1);,但这并没有成功。我四处搜寻,但找不到合适的条款。处理这个问题的最佳方法是什么?

4

1 回答 1

0

为什么不试试下面的代码?

#define LOG 1

static int logoutput(char *info) {
    FILE *dbg = NULL;
    dbg = fopen(LOGFILE, "a+");
    if(dbg != NULL) {
        fprintf(dbg, "Blah%d: %s\n", PARTNUM, info);
        fclose(dbg);
    }
    else {
        printf("Error writing to log file.\n");
    }
    return 0;//all good!
}    

static void logoutput_exit(char *info) {
   while(logoutput(info) != 0);
   exit(1);
}

if (data[0] != NULL) {
    if(LOG) logoutput_exit("WARNING, something bad happened on port 1!");
    //Adding a sleep(1); before this didn't help.
}
于 2013-02-20T03:23:17.100 回答