title: Count words in each line of text file in c++
body: <p>I open a txt file using argc, argv, and getline. I did that properly, but now I have to get the number of words per line (the number of lines is not known before) and I must output them in reverse. Meaning from the bottom line to the top line. Any help is appreciated. This code outputs the number of words in the file:</p>
  
  <pre><code>    #include &lt;iostream&gt;
      #include &lt;fstream&gt;
      #include &lt;cstring&gt;
      using namespace std;
  
      int main(int argc, char *argv[])
      {
          if(argc &lt; 1){   
              cerr &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; "filename.txt" &lt;&lt; endl; 
          }
  
              ifstream ifile(argv[1]);
              if(ifile.fail()){
              cerr &lt;&lt; "Could not open file." &lt;&lt; endl;
              return 1;
              }
  
          int n;
          ifile &gt;&gt; n;
          cout &lt;&lt; n;
  
          int numberOfWords = 0;  
          string line;
          for(int i = 0; i &lt;=  n; i++){
              getline(ifile, line);
              cout &lt;&lt; line &lt;&lt; endl;
          }
  
  
  
          size_t i;
  
          if (isalpha(line[0])) {
              numberOfWords++;
          }
  
          for (i = 1; i &lt; line.length(); i++) {
              if ((isalpha(line[i])) &amp;&amp; (!isalpha(line[i-1]))) {
                  numberOfWords++;
              }
          }
  
  
  
          cout&lt;&lt;"The number of words in the line is : "&lt;&lt;numberOfWords&lt;&lt;endl;
  
          return 0;
  }
  </code></pre>