title: how to extract file names with .txt extension from a text file?
body: <p>I have a text file named <code>myfile.txt</code> which lists the contents of drive <code>D:\</code>. In my program, I have a functon which will read <code>myfile.txt</code>. It will extract filenames from the <code>.txt</code> extension. I don't know much C++, so can you make it please "simple"? I am confused about the starting position of the substring as how would I know from where it will start.</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;fstream&gt;
  
  using namespace std;
  
  int main(void)
  {
      system("cls");
      string temp;
      fstream file;
      file.open("D:\\myfile.txt", ios::in);
  
      while( file.good() )
      {     
          file&gt;&gt;temp;
          string str2, str3;
          size_t pos;
  
          str2 = temp.substr (4,4); // confused with this
  
          pos = temp.find(".txt");    // position of ".txt" in temp
          str3 = temp.substr (pos);  
  
          cout &lt;&lt; str2 &lt;&lt; ' ' &lt;&lt; str3 &lt;&lt; endl;
      }
  
      system("pause");
      return 0;
  }
  </code></pre>