简体   繁体   中英

How can I redirect console output to file?

I'm new to c. Is there any simple way to redirect all the console's output (printfs etc.) to a file using some general command line \\ linkage parameter (without having to modify any of the original code)?

If so what is the procedure?

Use shell output redirection

your-command > outputfile.txt

The standard error will still be output to the console. If you don't want that, use:

your-command > outputfile.txt 2>&1

or

your-command &> outputfile.txt

You should also look into the tee utility, which can make it redirect to two places at once.

On unices, you can also do:

your-command | tee output file.txt

That way you'll see the output and be able to interact with the program, while getting a hardcopy of the standard output (but not standard input, so it's not like a teletype session).

As mentioned above, you can use the > operator to redirect the output of your program to a file as in:

./program > out_file

Also, you can append data to an existing file (or create it if it doesnt exit already by using >> operator:

./program >> out_file

If you really want to learn more about the (awesome) features that the command line has to offer I would really recommend reading this book (and doing lots of programming :))

http://linuxcommand.org/

Enjoy!

在Unix shell中,你通常可以执行executable > file 2> &1 ,这意味着“将标准输出重定向到文件并将错误输出重定向到标准输出”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM