title: Reading stderr from a linux device, after writing to it from c++
body: <p>When i write to a linux driver / device, in this case i want to put the embedded linux device to sleep:</p>
  <pre class="lang-bash prettyprint-override"><code>echo &quot;mem&quot; &gt; /sys/power/state
  </code></pre>
  <p>I get an error on the terminal if the above command fails</p>
  <pre><code>[ 2593.061030] dpm_run_callback(): elan_touch_suspend+0x0/0x114 returns 1
  [ 2593.067578] PM: Device 0-0015 failed to suspend: error 1
  [ 2593.072994] PM: Some devices failed to suspend, or early wake event detected
  [ 2593.107845] ==== calc_soc_by_voltageMethod E60U22 ====
  </code></pre>
  <p>And i do this the same in c++:</p>
  <pre class="lang-cpp prettyprint-override"><code>int fd2 = open(&quot;/sys/power/state&quot;, O_RDWR);
  write(fd2, &quot;mem&quot;, 3);
  close(fd2);
  </code></pre>
  <p>If the above command fails, i get the same error on the terminal. now i want to get this error as a string in c++, in shell i can do something like this:</p>
  <pre class="lang-bash prettyprint-override"><code>echo &quot;mem&quot; &gt; /sys/power/state 2&gt;/tmp/sleep_error
  </code></pre>
  <p>But i cant figure this out in c++, I need to to try one more time if it fails</p>
  <p>What I tryied:</p>
  <p>Capturing <code>cerr</code> of the whole program, with <code>freopen</code> doesn't work. When I write to the device from another terminal, and do <code>cat /dev/stderr</code> from another, i get the output in the second one, I tryied to use it:</p>
  <pre class="lang-cpp prettyprint-override"><code>char byte[1000];
  int stderrdevice = open(&quot;/dev/stderr&quot;, O_RDONLY | O_NOCTTY);
  int fd2 = open(&quot;/sys/power/state&quot;, O_RDWR);
  write(fd2, &quot;mem&quot;, 3);
  close(fd2);
  
  ssize_t size = read(stderrdevice, &amp;byte, 1000);
  
  printf(&quot;Read byte %s\n&quot;, byte);
  </code></pre>
  <p>This doesn't work too. Any resources, documentation related to this are welcome</p>