title: count the total number of files present in directory using c++
body: <p>I am trying to get number of files in a directory through c++ for unix OS<br>
  I have this code</p>
  
  <pre><code>int i;
  i = (int)system("ls -l /root/opencv/*.png|wc -l");
  cout &lt;&lt; "Number of files " &lt;&lt; i &lt;&lt; endl;
  </code></pre>
  
  <p>But I am getting output as</p>
  
  <pre><code>21
  Number of files 0
  </code></pre>
  
  <p>How can I get <code>21</code> in <code>i</code></p>