如何检查 std::cin 缓冲区是否与终端或管道相关联?
显示 std::cin 与文件关联的命令行:
Application.exe < C:\output.txt > C:\result.txt
显示与终端关联的 std::cin 的命令行:
Application.exe
为此,我试图分析 in_avail()。但它没有帮助。
std::cin.rdbuf()->in_avail()
最佳答案
我找到了问题的答案。
首先,我必须以正确的方式提出我的问题。
我想问的问题:如何判断 std::cin 与终端或管道相关联。
在 Windows 上:
_isatty( _fileno(stdin) );
在 POSIX 上:
isatty( fileno(stdin) );
在 Windows 上:
_isatty
如果 stdin 与字符设备关联,则调用返回非零值。
在 POSIX 上:
isatty
如果标准输入指的是终端,则调用返回 1。
适用于 Linux 的 isatty man:http://man7.org/linux/man-pages/man3/isatty.3.html
_isatty man 在 MSDN 上:http://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
关于c++ - 如何检查 std::cin 是否与终端或管道关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272388/