title: how would i create a file output stream in c like stdout?
body: <p>if printf uses stdout but how would i write a print function using my own output stream?  i want to handle this stream with a OO-like structure but i can do that myself.  is this possible?  this for learning.</p>
  
  <p>would something like this work - i didnt test this code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdarg.h&gt;
  
  FILE* stdout2 = NULL;
  
  int init() {
      stdout2 = fopen("stdout.txt", "w");
      if (!stdout2) return -1;
      return 1;
  }
  
  void print(char* fmt, ...) {
      va_list fmt_args;
      va_start(fmt_args, fmt);
      char buffer[300];
      vsprintf(buffer, fmt, fmt_args);
      fprintf(stdout2, buffer);
      fflush(stdout2);
  }
  
  void close() {
      fclose(stdout2);
  }
  
  int main(int argc, char** argv) {
      init();
      print("hi"); // to console?
      close();
      return 0;
  }
  </code></pre>
  
  <p>how would i get printf(char*, ...) print to the console?  would i have to read the file in the same function?</p>