title: Using grep with execl()
body: <p>A little context for my project: We have an arbitrary number of files that need a separate process for each file then need to search using an exec() call to find every time a specific KEY is used. I know how to use grep from the command line using this command:</p>
  
  <pre><code>grep -o KEY FILENAME.txt | wc -l &gt; OUTPUT.txt
  </code></pre>
  
  <p>But I cannot figure out how to do this in c++.  I found a thread on here that gave me this line.  </p>
  
  <pre><code>execl("/bin/grep","grep",pattern,filename,NULL);
  </code></pre>
  
  <p>It compiles and runs so I think it works but the problem is I need to output the number of times the pattern occurred to a file and I tried the line below but expectedly it didn't work. It gave this error "grep: out.txt: No such file or directory"</p>
  
  <pre><code>execl("/bin/grep", "grep",pattern,fileName,output,NULL);
  </code></pre>
  
  <p>Here are the directions of this part of my project.  </p>
  
  <blockquote>
    <p>You can do this by means of the
    system call exec() , providing it with the path to the executable of the shell (typically, /bin/sh )
    and, as arguments of /bin/sh , the string -c and the string corresponding to the search command
    ( grep -o ... ).</p>
  </blockquote>
  
  <p>Some guidance here would be much appreciated!  </p>