Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can just:</p> <pre><code>fclose(stdout); fclose(stderr); </code></pre> <p>For anybody wondering why you might want to do this, this is a fairly common task for a daemon/service process on Unix.</p> <p><strong>However</strong> you should be aware that closing a file descriptor may have unintended consequences:</p> <ul> <li>When you open new files these now free descriptors will be used. So, for example, if you subsequently <code>fopen</code> that file descriptor (on Linux, at least) will replace fd 1, i.e. stdout. Any code that subsequently uses this will write to this file, which may not be what you intended.</li> <li>See R..'s comments on file descriptors versus C library <code>FILE*</code> pointers. Specifically: <ul> <li>If you write to a closed fd under Linux, you'll get an error, but:</li> <li>If you use a C library function that uses <code>stdout</code> or <code>stderr</code> (which are <code>FILE*</code> pointers (see <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/stdin.html" rel="noreferrer">their definition</a>) then writing to these whilst <code>FILE*</code> is closed is undefined behaviour. This will likely crash your program in unexpected ways, not always at the point of the bug either. See <a href="http://en.wikipedia.org/wiki/Undefined_behavior" rel="noreferrer">undefined behaviour</a>.</li> </ul></li> <li>Your code isn't the only part affected. Any libraries you use, and any processes you launch which inherited these file descriptors as their standard descriptors are also affected.</li> </ul> <p>The quick, one-line solution is to <a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/freopen.html" rel="noreferrer"><code>freopen()</code></a> To say <code>/dev/null</code>, <code>/dev/console</code> under Linux/OSX or <code>nul</code> on Windows. Alternatively, you can use your platform-specific implementation to re-open the file descriptors/handles as required.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload