title: How do I count the number of words in a text file using C?
body: <p>I need some help with a program that I am writing for my Systems Programming class.  It is in C and I have very, very little experience with C.  I need to merge three text file with the format:</p>
  
  <pre><code>word1
  word2
  word3
  ...
  wordX
  </code></pre>
  
  <p>I am also to bring each of the words from all three files and put them into a 2D array (an array of string-arrays), then use some sort of sorting method on them.</p>
  
  <p>I shouldn't need help with the sorting, but I don't know how to get the word count from each of the text files or put them into an array.</p>
  
  <hr>
  
  <p>This is the function I have for counting the words in the file. It doesn't compile on <code>gcc</code> (probably for obvious reasons, but I don't know them).  Do I even have the right idea?</p>
  
  <pre><code>int countWords(FILE f){
     int count = 0;
     char ch;
     while ((ch = fgetc(f)) != EOF){
         if (ch == '\n')
             count++;
         //return count; originally here, but shouldn't be.
     }
         return count;
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT: I supposed I could just find a way to count the lines in the program, but I'm not sure if the approach would be any different from what I am trying to do here.  (I have never really been that good at working with text files.</p>
  
  <hr>
  
  <p>I got it to count all of the lines in the program.  I guess I'm a little rusty.</p>
  
  <hr>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  int countWords(FILE *f){
     int count = 0;
     char ch;
     while ((ch = fgetc(f)) != EOF){
         if (ch == '\n')
             count++;
     }
     return count;
  }
  int main(void){
  
     int wordCount = 0;
     FILE *rFile = fopen("american0.txt", "r");
     wordCount += countWords(rFile);
     printf("%d", wordCount);
     return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>I kind of forgot about that the pointer thing with <code>FILE *fileName</code></p>