我之前遇到过这样的事情,但通常只是在处理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);
但是没有做到这一点。我四处寻找,但无法提出正确的条款。处理此问题的最佳方法是什么?
答案 0 :(得分: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.
}