测试 stdin 是否有 C++ 输入(windows 和/或 linux)

2021-12-15 00:00:00 testing stdin pipe c++

我基本上想测试 stdin 是否有输入(就像你回声和管道输入一样).我找到了有效的解决方案,但它们很丑陋,我喜欢我的解决方案干净.

I basically want to test if stdin has input (like if you echo and pipe it). I have found solutions that work, but they are ugly, and I like my solutions to be clean.

在 linux 上我使用这个:

On linux I use this:

bool StdinOpen() {
  FILE* handle = popen("test -p /dev/stdin", "r");
  return pclose(handle) == 0;
}

我知道我应该添加更多的错误处理,但这不是重点.

I know that I should add more error handling, but it's besides the point.

在 Windows 上我使用这个:

On windows I use this:

bool StdinOpen() {
  static HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
  DWORD bytes_left;
  PeekNamedPipe(handle, NULL, 0, NULL, &bytes_left, NULL);
  return bytes_left;
}

这对 linux 来说很好,但我想知道我可以在不使用管道的情况下调用的等效 API 是什么(例如对于 test -f $file,您执行 fopen($文件,"r") != NULL).我有一种暗示,我可以 open("/dev/stdin", "r") 并做同样的事情,但我想知道最好的方法.

That is fine for linux, but I want to know what are the equivalent APIs that I can call without using a pipe (like for test -f $file you do fopen($file, "r") != NULL). I have an inkling that I could open("/dev/stdin", "r") and do the same thing, but I want to know the best way to do it.

总结:我想知道我可以用来代替 Linux 的 test -p/dev/stdin 的 API,如果你知道更好的解决方案窗户.

Summary: I want to know the APIs I could use to substitute for test -p /dev/stdin for linux, and, if you know a better solution for windows.

推荐答案

这是 POSIX (Linux) 的解决方案:我不确定 Windows 上的 poll() 等价物是什么.在 Unix 上,编号为 0 的文件描述符是标准输入.

Here's a solution for POSIX (Linux): I'm not sure what's the equivalent of poll() on Windows. On Unix, The file descriptor with number 0 is the standard input.

#include <stdio.h>
#include <sys/poll.h>

int main(void)
{
        struct pollfd fds;
        int ret;
        fds.fd = 0; /* this is STDIN */
        fds.events = POLLIN;
        ret = poll(&fds, 1, 0);
        if(ret == 1)
                printf("Yep
");
        else if(ret == 0)
                printf("No
");
        else
                printf("Error
");
        return 0;
}

测试:

$ ./stdin
No
$ echo "foo" | ./stdin
Yep

相关文章