Import stdout, stderr to the specified file

xiaoxiao2021-03-06  67

Import stdout, stderr to the specified file

In writing programs, STDOUT, STDERR is often used as an output, and usually STDOUT is used to output the information when the program is operating properly, and the STDERR is used to output the information when the program is wrong. We can import STDOUT, Stderr to output to the specified file by some commands, output on the screen.

1, ready to work

Test.cpp with stdout, stderr output

/ *

** Test.cpp

* /

#include

#include

Using namespace std;

Int main (int Argc, char * argv [])

{

Cout << "this is stdout ...." << Endl;

Cerr << "this is stderr ..." << endl;

Return 0;

}

Compile this file with G :

$ G Test.cpp -o test

Execute TEST, print STDOUT, STDERR information on the screen

This is stdout ....

This is stderr ...

2. Import the result of stdout to the specified file

Under CSH, SH, use ">" to import the results of stdout to the specified file, in this case

./test> Out.log

View Out.log, you can see "this is stdout ...." inside.

Use ">>" to append the results of stdout to the specified file, this example is executed

./test >> Out.log

View Out.log, you can see the increase of "this is stdout ....".

3, import stderr to the specified file

In CSH, use ">" to guide StDOUT to STDERR with STDERR with "> &". But you can't only turn the stderr. The best way is

(./TEST> OUT.LOG)> & Err.log

The above command will open a subshell execution "./test"; this Subshell's stdout is turned to Out.log, and this Subshell's stdout and stderr are turned to Err.log, but because STDOUT has been turned first. So stderr will be turned to Err.log.

If you just don't want to turn stdout, use sh to help you.

SH -C './test 2> Err.log'

If you need to get STDOUT, STDERR, it is also very simple, direct use> & you can.

./test> & err.log

转载请注明原文地址:https://www.9cbs.com/read-86688.html

New Post(0)