P
P
Pavel Kulaga2019-07-11 11:58:57
C++ / C#
Pavel Kulaga, 2019-07-11 11:58:57

How to determine inside the program where the output goes to the terminal or to the file?

Hello.
If you write ls in the terminal, for example, it will display something like this:

Makefile ft_ls libft output2.txt
author inc output.txt src

If you write in the terminal ls > out.txt, for example, it will display something like this:
Makefile
author
ft_ls
inc
libft
output.txt
output2.txt
src

That is, in the second case, it inserts only a line break, in the first case it also inserts spaces.
Maybe someone knows how it determines where it writes to a file or terminal in order to act differently?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
lil_toady, 2019-07-11
@hapr-lera

The author noticed that the output is different, that he himself sent the output to the file, he is aware :)
You can find out if we are writing to the terminal like this: istty is in io.h, and fileno is in stdio.h And ls, it really looks if the output is in the terminal - then in one line, otherwise - in several: https://github.com/coreutils/coreutils/blob/master...

S
Sergey, 2019-07-11
@feanor7

It doesn't define anything. It is you who determines where the output will go, cmd > file You wrote)))
In the usual case, the command will spit out everything to the standard output.

I
irishmann, 2019-07-11
@irishmann

So you yourself let her know that you are redirecting the output with the symbol ">"
I / O redirection in Linux

D
Dmitry, 2019-07-11
@dmtrrr

man ls

-1 (The numeric digit ``one''.) Force output to be one entry per line. This is the default when output is not to a terminal

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question