[Linux Applications] Linux application output data redirected to File

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.