How to know when there is input through the terminal pipe line on C++ 11?
I may call my program like this:
1. ./main solved.txt
2. cat unsolved.txt | ./main
3. cat unsolved.txt | ./main solved.txt
I am using this to know whether I need to read data from the pipe line or not on C POSIX Standard:
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <unistd.h>
int main( int argumentsCount, char* argumentsStringList[] )
{
std::stringstream inputedPipeLineString;
if( argumentsCount > 1 )
{
printf( "argumentsStringList[1]: %s", argumentsStringList[ 1 ] );
}
// If it is passed input through the terminal pipe line, get it.
if( !isatty( fileno( stdin ) ) )
{
// Converts the std::fstream "std::cin" to std::stringstream which natively
// supports conversion to string.
inputedPipeLineString << std::cin.rdbuf();
printf( "inputedPipeLineString: %s", inputedPipeLineString.str().c_str() );
}
}
But now I want to use the C++ 11 Standard, and my loved fileno
and isatty
are out of it. So there is an alternative to them on the C++ 11?
Related threads:
- checking data availability before calling std::getline
- Why does in_avail() output zero even if the stream has some char?
- Error "'fdopen' was not declared" found with g++ 4 that compiled with g++3
- stdio.h not standard in C++?
- error: ‘fileno’ was not declared in this scope
- GoogleTest 1.6 with Cygwin 1.7 compile error: 'fileno' was not declared in this scope
The problem is that when compiling with the -std=C++11
, the fileno
and isatty
are undefined on the stdio.h/cstdlib
because they are POSIX stuff. So, one solution would be to use -std=GNU++11
instead of -std=C++11
. But is it possible to write something else to compile using the -std=C++11
?