title: How to write console output to a text file
body: <p>I have this key generating algorithm made in C that will display all generated keys in console:</p>
  
  <p>So how can I save all the keys written to console to a text file?</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdlib.h&gt;
  
  static const char alphabet[] = "abcdefghijklmnopqrs";               
  static const int alphabet_size = sizeof(alphabet) - 1;
  
  void brute_impl(char * str, int index, int max_depth)
  {
      int i;
      for (i = 0; i &lt; alphabet_size; ++i)
      {
          str[index] = alphabet[i];
  
          if (index == max_depth - 1)
          {   
              printf("%s\n", str); // put check() here instead                     
          }
          else
          {
              brute_impl(str, index + 1, max_depth);
          }
      }
  }
  
  void brute_sequential(int max_len)
  {
      char * buf = malloc(max_len + 1);
      int i;
  
      for (i = 8; i &lt;= max_len; ++i)
      {
      memset(buf, 0, max_len + 1);
      brute_impl(buf, 0, i);
      }
      free(buf);
  }
  
  int main(void)
  {
      brute_sequential(8);
      return 0;
  }
  </code></pre>