如何通过C 11端子管线输入什么?
我可以像这样调用我的程序:
1. ./main solved.txt 2. cat unsolved.txt | ./main 3. cat unsolved.txt | ./main solved.txt
我正在使用它来知道是否需要从C行POSIX标准中读取管道中的数据:
#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() ); } }
但是现在我想使用C11标准,而我所爱的fileno和isatty也是这样的.那么在C 11上有一个替代方案呢?
相关主题:
> 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
问题是当使用-std = C 11编译时,在stdio.h / cstdlib上,fileno和isatty是未定义的,因为它们是POSIX的东西.所以,一个解决方案是使用-std = GNU 11而不是-std = C 11.但是可以使用-std = C 11编写别的东西来编译吗?