C++ 检测stdin是终端还是管道?
当我在没有参数的终端上执行“C++ 检测stdin是终端还是管道?,c++,c,qt,pipe,stdin,C++,C,Qt,Pipe,Stdin,当我在没有参数的终端上执行“python”时,它会显示python交互式shell 当我从终端执行“cat | python”时,它不会启动交互模式。不知何故,在没有任何输入的情况下,它检测到它已连接到管道 < P> >在C或C++或Qt? < P>调用Stand()或FSTATE()中,我将如何进行类似的检查,并查看SyIFIFO是否设置在STYMODE中。< /P> < P> STATE()或FSTATE(),并查看SyIFIFO是否设置为STYMODE。 < P>可能是检查FSTAT中“S
python
”时,它会显示python交互式shell
当我从终端执行“cat | python
”时,它不会启动交互模式。不知何故,在没有任何输入的情况下,它检测到它已连接到管道
< P> >在C或C++或Qt? < P>调用Stand()或FSTATE()中,我将如何进行类似的检查,并查看SyIFIFO是否设置在STYMODE中。< /P> < P> STATE()或FSTATE(),并查看SyIFIFO是否设置为STYMODE。 < P>可能是检查FSTAT中“STDIN”的文件类型,类似于:
struct stat stats;
fstat(0, &stats);
if (S_ISCHR(stats.st_mode)) {
// Looks like a tty, so we're in interactive mode.
} else if (S_ISFIFO(stats.st_mode)) {
// Looks like a pipe, so we're in non-interactive mode.
}
当然,Python是开放源码的,因此您只需看看它们的功能,就可以肯定地知道:
可能他们正在用fstat检查“stdin”的文件类型,如下所示:
struct stat stats;
fstat(0, &stats);
if (S_ISCHR(stats.st_mode)) {
// Looks like a tty, so we're in interactive mode.
} else if (S_ISFIFO(stats.st_mode)) {
// Looks like a pipe, so we're in non-interactive mode.
}
当然,Python是开放源码的,因此您只需看看它们的功能,就可以肯定地知道:
您可以调用
stat(0,&result)
并检查!S_ISREG(result.st_模式)
。这是Posix,而不是C/C++。您可以调用stat(0,&result)
并检查!S_ISREG(result.st_模式)
。这是Posix,而不是C/C++。使用isatty
:
#include <stdio.h>
#include <io.h>
...
if (isatty(fileno(stdin)))
printf( "stdin is a terminal\n" );
else
printf( "stdin is a file or a pipe\n");
#包括
#包括
...
if(isatty(fileno(stdin)))
printf(“stdin是一个终端\n”);
其他的
printf(“stdin是一个文件或管道\n”);
(在windows上,它们的前缀带有下划线:
\u isatty
,\u fileno
)使用isatty
:
#include <stdio.h>
#include <io.h>
...
if (isatty(fileno(stdin)))
printf( "stdin is a terminal\n" );
else
printf( "stdin is a file or a pipe\n");
#包括
#包括
...
if(isatty(fileno(stdin)))
printf(“stdin是一个终端\n”);
其他的
printf(“stdin是一个文件或管道\n”);
(在windows上,它们的前缀带有下划线:\u isatty
,\u fileno
)摘要
对于许多用例,该功能就是检测stdin是否连接到终端所需的全部功能。一个简单的例子:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if (isatty(fileno(stdin)))
puts("stdin is connected to a terminal");
else
puts("stdin is NOT connected to a terminal");
return 0;
}
调用Py\u FdIsInteractive()
/*
*如果出现以下情况之一,则文件描述符fd被视为“交互式”
*a)isatty(fd)为真,或
*b)给出了-i标志,并且文件名与
*描述符为NULL或“或”。
*/
int
Py_FdIsInteractive(文件*fp,常量字符*文件名)
{
if(isatty((int)fileno(fp)))
返回1;
它调用isatty()
结论
有不同程度的交互。用于检查stdin
是否
连接到管道/文件或实际终端isatty()
是一种自然的方法
这样做。总结
对于许多用例,该功能是检测stdin是否连接到终端所需的全部功能。一个简单的示例:
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if (isatty(fileno(stdin)))
puts("stdin is connected to a terminal");
else
puts("stdin is NOT connected to a terminal");
return 0;
}
调用Py\u FdIsInteractive()
/*
*如果出现以下情况之一,则文件描述符fd被视为“交互式”
*a)isatty(fd)为真,或
*b)给出了-i标志,并且文件名与
*描述符为NULL或“或”。
*/
int
Py_FdIsInteractive(文件*fp,常量字符*文件名)
{
if(isatty((int)fileno(fp)))
返回1;
它调用isatty()
结论
有不同程度的交互。用于检查stdin
是否
连接到管道/文件或实际终端isatty()
是一种自然的方法
这样做。在Windows上,您可以使用GetFileType
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD type = GetFileType(hIn);
switch (type) {
case FILE_TYPE_CHAR:
// it's from a character device, almost certainly the console
case FILE_TYPE_DISK:
// redirected from a file
case FILE_TYPE_PIPE:
// piped from another program, a la "echo hello | myprog"
case FILE_TYPE_UNKNOWN:
// this shouldn't be happening...
}
在Windows上,可以使用GetFileType
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD type = GetFileType(hIn);
switch (type) {
case FILE_TYPE_CHAR:
// it's from a character device, almost certainly the console
case FILE_TYPE_DISK:
// redirected from a file
case FILE_TYPE_PIPE:
// piped from another program, a la "echo hello | myprog"
case FILE_TYPE_UNKNOWN:
// this shouldn't be happening...
}
+1:stdin可以是管道或从文件重定向。检查它是否是交互式的比检查它是否是交互式的更好。在POSIX上没有
io.h
,对于isatty()
,您需要包括unistd.h
。后续问题:如果stdin不是tty,如何读取管道内容?注意:您需要检查stdout(STDOUT_FILENO)如果您想查看您的-output-是否是tty,如果您想通过管道将其传输到less
+1,则禁止输出:stdin可以是管道或从文件重定向。检查它是否是交互式的比检查它是否不是更好。在POSIX上没有io.h
和isatty()
您需要包括unistd.h
。后续问题:如果stdin不是tty,如何读取管道内容?注意:您需要检查stdout(stdout\u文件号)如果要查看您的-output-是否为tty,以防在管道中插入less
时禁止输出。您不想检测stdin是否为管道,而是想检测stdin/stdout是否为终端。您不想检测stdin是否为管道,而是想检测stdin/stdout是否为终端。