title: Most Compact Way to Count Number of Lines in a File in C++
body: <p>What's the most compact way to compute the number of lines of a file?
  I need this information to create/initialize a matrix data structure.</p>
  
  <p>Later I have to go through the file again and store the information inside a matrix.</p>
  
  <p><strong>Update:</strong> Based on Dave Gamble's. But why this doesn't compile?
  Note that the file could be very large. So I try to avoid using container
  to save memory. </p>
  
  <pre><code>#include &lt;iostream&gt;      
  #include &lt;vector&gt;        
  #include &lt;fstream&gt;       
  #include &lt;sstream&gt;       
  using namespace std;     
  
  
  int main  ( int arg_count, char *arg_vec[] ) {
      if (arg_count !=2 ) {
          cerr &lt;&lt; "expected one argument" &lt;&lt; endl;
          return EXIT_FAILURE;      
      }
  
      string line;
      ifstream myfile (arg_vec[1]);
  
      FILE *f=fopen(myfile,"rb");
      int c=0,b;
      while ((b=fgetc(f))!=EOF) c+=(b==10)?1:0;
      fseek(f,0,SEEK_SET);
  
  
      return 0;
  }
  </code></pre>