transferred from : http://blog.chinaunix.net/uid-20680966-id-4698387.html
The purpose is to let the program's printf print can be redirected to a text, CTRL + C Force exit after viewing the text, convenient debugging.
Operating mode:
A. The out-of-process function prints some characters
A.out12>&1
The stdout and stderr are redirected to the 1.txt file, the results found that the operation of 1.txt has been empty, debugging found if the A.out normal end exit 1.txt data is normal,
CTRL + C Force quit the program is 1.txt no data, think of the reason for the cache, not written to the store.
But adding the sync () function to the program has no effect,
Workaround :
For example, a moment I want to CTRL + C exit the program to see 1.txt content, the program added to the detection of SIGINT, plus exit () function
void handle_sig (int num) { "%s\n", __func__); Exit (1);} int intChar * *argv) { signal (SIGINT, handle_sig); // .......................}
A better approach is to replace printf with a function that writes log, writing text directly without redirecting.
Too handsome to go out. Programmer Group: 31843264
[Linux Applications] Linux application output data redirected to File