How to know when there is input through the terminal pipe line on C++ 11?
Solution 1:
C++ POSIX Standard
As far as I know, there is no such thing. There is a C POSIX library, which is part of POSIX standard.
So there is an alternative to them on the C++ 11?
There is no alternative in standard C++ (not before C++11, so far not after either).
You will need to depend on POSIX to get the functionality that you need.
Even in POSIX, it is the unistd.h
which defines isatty
. You've neglected to include it in your program.
Solution 2:
I'm not aware of a completely portable way to do this. As far as I know, standard C++ knows no information of where it's input comes from so you should just use isatty
if you are working on a posix system.
Solution 3:
In C++17, you have the <filesystem>
header which has a std::filesystem::status()
function that returns a file_status
object. You can access its type through the type()
function, via the result for instance, and use a quick comparison to see if it's a type of your intended target.
One really naïve approach to this would be something like:
auto result {std::filesystem::status(fd)};
if (result.type() == std::filesystem::file_type::character)
{
// do some stuff in here
}
where fd is the file descriptor to check. The above isn't full proof since there's no additional checks, but if it's a character type, it can almost certainly be equated to a terminal.
If you have a compiler that supports C++17, <filesystem>
can be really handy
https://en.cppreference.com/w/cpp/filesystem/file_type
NOTE: I am just starting out with working with <filesystem>
so there may be some nuances I'm missing here and welcome any updates to the answer