[英]C program exits before logging
我以前遇到過這種情況,但通常只是在處理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);
但這並不能解決問題。 我四處搜尋,但找不到正確的字詞。 處理此問題的最佳方法是什么?
為什么不嘗試以下代碼?
#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.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.