Printing output of c program to a file in Linux

www.la‮oc.iruttu‬m
Printing output of c program to a file in Linux

To redirect the output of a C program to a file in Linux, you can use the > operator in the command line.

For example, to compile and run a C program called myprogram.c and save the output to a file called output.txt, you can use the following command:

gcc myprogram.c -o myprogram && ./myprogram > output.txt

This command first compiles the C program using the gcc compiler and creates an executable file called myprogram. The && operator runs the executable file and redirects its output to the file output.txt.

You can also use the tee command to redirect the output to both the screen and a file:

./myprogram | tee output.txt

Keep in mind that the > operator overwrites the contents of the file if it already exists. To append the output to the end of the file instead, you can use the >> operator.

./myprogram >> output.txt

Consult the documentation and online resources for more information on redirecting output in Linux and other advanced shell commands.

Created Time:2017-10-30 14:27:12  Author:lautturi