title: Test if stdin has input for C++ (windows and/or linux)
body: <p>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.</p>
  
  <p>On linux I use this:</p>
  
  <pre><code>bool StdinOpen() {
    FILE* handle = popen("test -p /dev/stdin", "r");
    return pclose(handle) == 0;
  }
  </code></pre>
  
  <p>I know that I should add more error handling, but it's besides the point.</p>
  
  <p>On windows I use this:</p>
  
  <pre><code>bool StdinOpen() {
    static HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
    DWORD bytes_left;
    PeekNamedPipe(handle, NULL, 0, NULL, &amp;bytes_left, NULL);
    return bytes_left;
  }
  </code></pre>
  
  <p>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 <code>test -f $file</code> you do <code>fopen($file, "r") != NULL</code>). I have an inkling that I could <code>open("/dev/stdin", "r")</code> and do the same thing, but I want to know the best way to do it.</p>
  
  <p><strong>Summary:</strong> I want to know the APIs I could use to substitute for <code>test -p /dev/stdin</code> for linux, and, if you know a better solution for windows.</p>