我怎么知道命令行是: 。/主要的 或者: ./main <文件名
最佳答案
通常你不应该注意或关心,但如果你真的需要,你可以使用类似
if (isatty(fileno(stdin)))
// getting input from console
else
// probably getting input from a file.
isatty
或 fileno
都不是 C 本身的标准化部分,但它们都包含在 POSIX 中(如果有内存)并且在大多数非 POSIX 系统上也可用(例如,在 Windows 上,尽管 MS 认为这些名称已弃用,并建议分别使用 _isatty
和 _fileno
)。
关于c++ - 如何使用管道检查是否有文件输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5454362/