D
D
DDwrt1002021-08-02 17:10:19
C++ / C#
DDwrt100, 2021-08-02 17:10:19

How to capture console output from a C program?

Hello everyone, there is a small console program written in C.
While running, it displays information about its work in the terminal.
How can I redirect this output to a file?
For some reason, the standard Linux methods don't dump anything into the file. At the end of the work, the file is empty.

./app > output.txt
./app > tee -a output.txt


In the program itself, the output is organized using printf.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Dubrovin, 2021-08-02
@z3apa3a

Several options are possible:
1. The application writes to stderr, then
./app 2>stderr.txt
2. The application writes to stdout but does not read anything from stdin
by default, the output of fprintf is buffered and the buffer is only flushed when the buffer is full, waiting for input, or exiting the program. You can either turn off buffering
setvbuf(stdout, NULL, _IONBF, 0);
somewhere at the beginning of the program, or force the buffer to be flushed fflush(stdout);after each printf.

S
Saboteur, 2021-08-03
@saboteur_kiev

./app >>file.log 2>>&1
You can write to the terminal either to stdout or stderr, intercept both and redirect to a file, try with appending (>>)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question