title: Finding pattern in a text in C++
body: <p>I have written the following code to find the number of "ATA" in  a text that is read to a string as "GCTATAATAGCCATA". The count returned should be 3 but it returns 0. When I check in debugger the string for text is initially created. However, when an empty string is passed to the function patternCount. Am I reading the contents of the file into the string text correctly? </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  void  patternCount(string text, string pattern);
  
  int main()
  {
      string text;
      fstream file_("test.txt");
      if(file_.is_open())
      {
          while(getline(file_,text))
          {
              cout &lt;&lt; text &lt;&lt; '\n';
          }
          file_.close();
      }
      cout &lt;&lt; "Enter a string ";
      string pattern;
      getline(cin, pattern);
      patternCount(text, pattern);
  
  
      return 0;
  }
  
  void patternCount(string text, string pattern)
  {
      int count = 0;
      size_t nPos = text.find(pattern, 0);
      while (nPos != string::npos)
      {
          nPos = text.find(pattern, nPos + 1);
          ++count;
      }
      cout &lt;&lt; "There are " &lt;&lt; count &lt;&lt;" " &lt;&lt; pattern &lt;&lt; " in your text.\n";
  }
  </code></pre>