[問題] 如何強迫 printf 寫回資料

看板Programming作者 (NII)時間18年前 (2007/03/02 10:10), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
一個簡單的測試程式,我將 stdout 轉向到檔案 log,程式執行後,會停在 loop 裡,按下 ctrl+z 或 ctrl+c 強迫程式結束。 檢查 log 檔, ls 的結果都正確寫入,可是字串HELLO卻沒有正確寫入,我已經呼叫 fsync() 強迫將 buffer 資料寫回了,為甚麼還是沒有? 是不是有甚麼地方我沒有注意到呢,謝謝囉 #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> int main(void) { int fd; FILE *fp = NULL; if ( (fp=fopen("log", "w")) == NULL ) { printf("fopen error"); exit(1); } fd = fileno(fp); if ( dup2(fd, STDOUT_FILENO) == -1 ) { fprintf(stderr, "Redirect Standard Out error"); exit(1); } printf("HELLO\n"); fsync(fd); system("ls"); while(1); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.124.92.123
文章代碼(AID): #15vuULPX (Programming)
文章代碼(AID): #15vuULPX (Programming)