Print on terminal and into file simultaneously?
Pipe your output to the tee
command.
Example:
[me@home]$ echo hello | tee out.txt
hello
[me@home]$ cat out.txt
hello
Note that the stdout of echo
is printed out as well as written to the file specified by thr tee
command.
How to show output on terminal and save to a file at the same time in C?
Ways to solve printing to the terminal and saving output to file
- Using
printf()
, orfprintf(stdin, "");
to stdin, statement afterfprintf();
- Using
system("cat output.log");
(works in linux) after writing to file. - Using function which will print the file after writing like
#include <stdio.h>
void printFile(FILE *fp) {
char line[2048];
while (fscanf(fp, "%[^\n]s", line) == 1) {
printf("%s\n", line);
fgetc(fp); // OR fseek(fp, 1, 1); To throw away the new line in the input
// buffer
}
}
int main() {
FILE *fp;
fp = fopen("a.txt", "r");
if (fp == NULL) {
printf("Error opening the file\n");
return 1;
}
printFile(fp);
return 0;
}
- Using linux shell
./a.out | tee output.log
and use normal printf() statement inside your C code.
linux terminal: how to copy output to a file and print at terminal simultaneously?
Use tee command to achieve the same.
command | tee output.txt
Displaying stdout on screen and a file simultaneously
I can't say why tail
lags, but you can use tee
:
Redirect output to multiple files, copies standard input to standard output and also to any files given as arguments. This is useful when you want not only to send some data down a pipe, but also to save a copy.
Example: <command> | tee <outputFile>
Direct output to standard output and an output file simultaneously
Use tee
:
./executable 2>&1 | tee outputfile
tee
outputs in chunks and there may be some delay before you see any output. If you want closer to real-time output, you could redirect to a file as you are now, and monitor it with tail -f
in a different shell:
./executable 2>&1 > outputfile
tail -f outputfile
Is is possible to log everything that is printed to the terminal into a text file in C?
You can do: ./executable > outputfile 2>&1
or ./executable &> outputfile
This will enable you to redirect both the standard output and the standard output to the outputfile
.
How to redirect output to a file and stdout
The command you want is named tee
:
foo | tee output.file
For example, if you only care about stdout:
ls -a | tee output.file
If you want to include stderr, do:
program [arguments...] 2>&1 | tee outfile
2>&1
redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. It is also directed to the given output file as of the tee
command.
Furthermore, if you want to append to the log file, use tee -a
as:
program [arguments...] 2>&1 | tee -a outfile
Related Topics
Differencebetween "Var=${Var:-Word}" and "Var=${Var:=Word}"
How to Add Chromedriver to Path in Linux
Shell Script Get Ctrl+Z with Trap
Compiling 32 Bit Assembler on 64 Bit Ubuntu
How to Name the 'Screen' Logfile from the -L Flag
How Much Does Using Htaccess Files Slow Down Website Performance (Especially with Solid State Disks)
Bash: Head & Tail Behavior with Bash Script
Is It Necessary to Deregister a Socket from Epoll Before Closing It
Webpack --Watch Exits After Building Once
Can Inode and Crtime Be Used as a Unique File Identifier
How to Reset Emacs to Save Files in Utf-8-Unix Character Encoding
What Is the Windows Equivalent of Linux Command Wc -L
How to Call a Function (Defined in Shell Script) in a Perl Script
Strange File Permission in Docker Container (Question Marks on Permission Bit and User Bit)
What Is the Most Efficient and Elegant Way Develop/Debug Linux Kernel
Sort Logs by Date Field in Bash
How to Find Out What Linux Capabilities a Process Requires to Work