• <tfoot id='G7DeL'></tfoot>

  • <i id='G7DeL'><tr id='G7DeL'><dt id='G7DeL'><q id='G7DeL'><span id='G7DeL'><b id='G7DeL'><form id='G7DeL'><ins id='G7DeL'></ins><ul id='G7DeL'></ul><sub id='G7DeL'></sub></form><legend id='G7DeL'></legend><bdo id='G7DeL'><pre id='G7DeL'><center id='G7DeL'></center></pre></bdo></b><th id='G7DeL'></th></span></q></dt></tr></i><div id='G7DeL'><tfoot id='G7DeL'></tfoot><dl id='G7DeL'><fieldset id='G7DeL'></fieldset></dl></div>
    1. <small id='G7DeL'></small><noframes id='G7DeL'>

      <legend id='G7DeL'><style id='G7DeL'><dir id='G7DeL'><q id='G7DeL'></q></dir></style></legend>
          <bdo id='G7DeL'></bdo><ul id='G7DeL'></ul>

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

        时间:2023-08-02
        • <tfoot id='aPGOQ'></tfoot>
        • <i id='aPGOQ'><tr id='aPGOQ'><dt id='aPGOQ'><q id='aPGOQ'><span id='aPGOQ'><b id='aPGOQ'><form id='aPGOQ'><ins id='aPGOQ'></ins><ul id='aPGOQ'></ul><sub id='aPGOQ'></sub></form><legend id='aPGOQ'></legend><bdo id='aPGOQ'><pre id='aPGOQ'><center id='aPGOQ'></center></pre></bdo></b><th id='aPGOQ'></th></span></q></dt></tr></i><div id='aPGOQ'><tfoot id='aPGOQ'></tfoot><dl id='aPGOQ'><fieldset id='aPGOQ'></fieldset></dl></div>

            <tbody id='aPGOQ'></tbody>

            1. <small id='aPGOQ'></small><noframes id='aPGOQ'>

              • <bdo id='aPGOQ'></bdo><ul id='aPGOQ'></ul>

                  <legend id='aPGOQ'><style id='aPGOQ'><dir id='aPGOQ'><q id='aPGOQ'></q></dir></style></legend>
                1. 本文介绍了测试 stdin 是否有 C++ 输入(windows 和/或 linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我基本上想测试 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
                  

                  这篇关于测试 stdin 是否有 C++ 输入(windows 和/或 linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 CMake/CDash 中使用 gcov 的详细指南? 下一篇:什么是 Google 测试、死亡测试

                  相关文章

                  最新文章

                  • <bdo id='K2XSt'></bdo><ul id='K2XSt'></ul>

                  <small id='K2XSt'></small><noframes id='K2XSt'>

                2. <i id='K2XSt'><tr id='K2XSt'><dt id='K2XSt'><q id='K2XSt'><span id='K2XSt'><b id='K2XSt'><form id='K2XSt'><ins id='K2XSt'></ins><ul id='K2XSt'></ul><sub id='K2XSt'></sub></form><legend id='K2XSt'></legend><bdo id='K2XSt'><pre id='K2XSt'><center id='K2XSt'></center></pre></bdo></b><th id='K2XSt'></th></span></q></dt></tr></i><div id='K2XSt'><tfoot id='K2XSt'></tfoot><dl id='K2XSt'><fieldset id='K2XSt'></fieldset></dl></div>
                3. <tfoot id='K2XSt'></tfoot>

                    1. <legend id='K2XSt'><style id='K2XSt'><dir id='K2XSt'><q id='K2XSt'></q></dir></style></legend>