title: Printing to a file in C
body: <p>How do I print to an empty <code>.txt file</code> I already have created?</p>
  
  <p>I already print the results to the console, and now I want to print to a file named <code>"Output.txt"</code>. I've tried a couple of things that haven't worked, but I think it was easier to create a duplicate <code>printDictionary()</code> specifically for printing to a file called <code>printDictionaryToFile()</code>. I'm a little lost on how to do it though. Can anyone correct me on where I went wrong? I already added an extra <code>FILE</code> type called <code>*out</code> for my output to a file.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;stddef.h&gt;
  
  #define PUNC    " \t\n\r,;.:!()[]{}?'\""
  typedef struct node node;
  
  typedef struct node {
      char *word;
      int count;
      node *left;
      node *right;
  } node;
  
  
  void insert(node ** dictionary, char * word) {
      int result;
      node * entry;
  
      if (word == NULL || dictionary == NULL)
          return;
      if (*dictionary == NULL) {
          entry= (node *) malloc(sizeof(node));
          strcpy( entry-&gt;word= (char *) malloc(strlen(word) + 1), word);
          entry-&gt;left= entry-&gt;right= NULL;
          entry-&gt;count= 1;
          *dictionary= entry;
          return;
      }
      result = strcmp(word, (*dictionary)-&gt;word);
      if ( result &lt; 0 )
          insert(&amp;(*dictionary)-&gt;left, word);
      else if (result &gt; 0)
          insert(&amp;(*dictionary)-&gt;right, word);
      else
          ++(*dictionary)-&gt;count;
  
      return;
  }
  
  
  void printDictionary(node * dictionary) {
      if (dictionary == NULL)
          return;
      printDictionary(dictionary-&gt;left);
      printf( "%s = %d\n", dictionary-&gt;word, dictionary-&gt;count);
      printDictionary(dictionary-&gt;right);
  
      return;
  }
  
  
  void printDictionaryToFile( node * dictionary ) {
      if (dictionary == NULL)
          return;
      printDictionaryToFile(dictionary-&gt;left);
      fprintf(out, "%s = %d\n", dictionary-&gt;word, dictionary-&gt;count);
      printDictionaryToFile(dictionary-&gt;right);
      return;
  }
  
  
  void freeDictionary( node ** dictionary ) {
      if (dictionary == NULL || *dictionary == NULL)
          return;
      freeDictionary(&amp;(*dictionary)-&gt;left);
      freeDictionary(&amp;(*dictionary)-&gt;right);
      free((*dictionary)-&gt;word);
      free(*dictionary);
      *dictionary= NULL;
      return;
  }
  
  
  int main( int argc, char *argv[] ) {
      FILE *fp, *out;
      out = fopen("Output.txt", "w");
      char b[1000], *s;
      node *dictionary= NULL;
      int i;
  
      for (i= 1; i &lt; argc; ++i) {
          if ((fp = fopen(argv[i], "r")) == NULL) {
              fprintf(stderr, "File %s can not be opened.\n", argv[i]);
              continue;
          }
          for (s = fgets(b, sizeof(b), fp); s != NULL; s = fgets(b, sizeof(b), fp)) {
              char *word;
              for (word= strtok(b, PUNC); word != NULL; word = strtok(NULL, PUNC))
                  insert(&amp;dictionary, strlwr(word));
          }
          fclose(fp);
      }
      printDictionaryToFile(dictionary);
      printDictionary(dictionary);
      freeDictionary(&amp;dictionary);
      return 0;
  }
  </code></pre>