Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can save the global variable <code>stdout</code> before <code>bar();</code>, then set it to <code>/dev/null</code>, then restore it. Code example;</p> <pre><code>#include &lt;stdio.h&gt; int main() { int old_stdout = dup(1); freopen ("/dev/null", "w", stdout); // or "nul" instead of "/dev/null" printf("asd1"); fclose(stdout); stdout = fdopen(old_stdout, "w"); printf("asd2"); return 0; } </code></pre> <p>Tested on OS X, not sure about Windows.</p> <p><strong>EDIT:</strong> You can replace <code>/dev/null</code> with <code>nul</code> on Windows. As for the <code>fdopen()</code> and <code>dup()</code> working under Windows - they're part of the POSIX.1 standard according to my <code>man</code> pages, but I haven't tested it.</p> <p><strong>EDIT 2:</strong> As suggested in the comments, if the solution does not work for Windows, there's another suggestion in <a href="https://stackoverflow.com/a/7664951/1071136">this SO answer</a>.</p> <p><strong>EDIT 3:</strong> A longer, though standard compliant and cross-platform way to do this is as following. (code adapted from mentioned SO question, and Microsoft's documentation for _dup)</p> <pre><code>#include &lt;stdio.h&gt; #ifdef _WIN32 #include &lt;io.h&gt; char * const nulFileName = "NUL"; #define CROSS_DUP(fd) _dup(fd) #define CROSS_DUP2(fd, newfd) _dup2(fd, newfd) #else #include &lt;unistd.h&gt; char * const nulFileName = "/dev/null"; #define CROSS_DUP(fd) dup(fd) #define CROSS_DUP2(fd, newfd) dup2(fd, newfd) #endif int main() { int stdoutBackupFd; FILE *nullOut; /* duplicate stdout */ stdoutBackupFd = CROSS_DUP(STDOUT_FILENO); fflush(stdout); nullOut = fopen(nulFileName, "w"); CROSS_DUP2(fileno(nullOut), STDOUT_FILENO); printf("asd1\n"); fflush(stdout); fclose(nullOut); // Restore stdout CROSS_DUP2(stdoutBackupFd, STDOUT_FILENO); close(stdoutBackupFd); printf("asd2\n"); return 0; } </code></pre> <p>The flushes are required to make sure that (1) anything printed before switching stdout is indeed printed to screen, and (2) nothing that was printed before switching back stdout to console (or pipe) is printed.</p>
    singulars
    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. 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