Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <code>fdopen()</code> and <code>dup()</code> as well as <code>freopen()</code>.</p> <pre><code>int old_stdout = dup(1); // Preserve original file descriptor for stdout. FILE *fp1 = freopen("out.txt", "w", stdout); // Open new stdout ...write to stdout... // Use new stdout FILE *fp2 = fdopen(old_stdout, "w"); // Open old stdout as a stream ...Now, how to get stdout to refer to fp2? ...Under glibc, I believe you can use: fclose(stdout); // Equivalent to fclose(fp1); stdout = fp2; // Assign fp2 to stdout // *stdout = *fp2; // Works on Solaris and MacOS X, might work elsewhere. close(old_stdout); // Close the file descriptor so pipes work sanely </code></pre> <p>I'm not sure whether you can do the assignment reliably elsewhere.</p> <h3>Dubious code that does actually work</h3> <p>The code below worked on Solaris 10 and MacOS X 10.6.2 - but I'm not confident that it is reliable. The structure assignment may or may not work with Linux glibc.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;unistd.h&gt; int main(void) { printf("This goes to screen\n"); int old_stdout = dup(1); // Consider dup(STDOUT_FILENO) or dup(fileno(stdout)) FILE *fp1 = freopen("out.txt", "a", stdout); printf("This goes to out.txt\n"); fclose(stdout); FILE *fp2 = fdopen(old_stdout, "w"); *stdout = *fp2; // Unreliable! printf("This should go to screen too, but doesn't\n"); return 0; } </code></pre> <p>You can't say you weren't warned — <strong>this is playing with fire!</strong></p> <p>If you're on a system with the <code>/dev/fd</code> file system, you could create the name of the file implied by the file descriptor returned from <code>dup()</code> with <code>sprintf(buffer, "/dev/fd/%d", old_stdout)</code> and then use <code>freopen()</code> with that name. This would be a lot more reliable than the assignment used in this code.</p> <p>The better solutions either make the code use 'fprintf(fp, ...)' everywhere, or use a cover function that allows you set your own default file pointer:</p> <h3>mprintf.c</h3> <pre><code>#include "mprintf.h" #include &lt;stdarg.h&gt; static FILE *default_fp = 0; void set_default_stream(FILE *fp) { default_fp = fp; } int mprintf(const char *fmt, ...) { va_list args; va_start(args, fmt); if (default_fp == 0) default_fp = stdout; int rv = vfprintf(default_fp, fmt, args); va_end(args); return(rv); } </code></pre> <h3>mprintf.h</h3> <pre><code>#ifndef MPRINTF_H_INCLUDED #define MPRINTF_H_INCLUDED #include &lt;stdio.h&gt; extern void set_default_stream(FILE *fp); extern int mprintf(const char *fmt, ...); #endif </code></pre> <p>Clearly, you can create an mvprintf() and other functions as needed.</p> <h3>Example use of mprintf()</h3> <p>Then, in place of the original code, you can use:</p> <pre><code>#include "mprintf.h" int main() { mprintf("This goes to screen\n"); FILE *fp1 = fopen("out.txt", "w"); set_default_stream(fp1); mprintf("This goes to out.txt\n"); fclose(fp1); set_default_stream(stdout); mprintf("This should go to screen too, but doesn't\n"); return 0; } </code></pre> <p>(Warning: untested code - confidence level too high. Also, all code written assuming you use a C99 compiler, primarily because I declare variables when I first need them, not at the beginning of the function.)</p> <hr> <p>Caution:</p> <p>Note that if the original program is invoked as <code>./original_program &gt; file</code> or <code>./original_program | grep something</code> (with redirected output) or is run from a <code>cron</code> job, then opening <code>/dev/tty</code> is not usually appropriate as a way to reopen standard output because the original standard output was not the terminal.</p> <p>Also, note that if the redirection of standard output is used prior to forking and execing a child program and the original standard output is reinstated in the parent, then the sequence of operations is wrong. You should fork and then adjust the I/O of the child (only), without modifying the parent's I/O at all.</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. 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