title: Findind more than one word in a file and then extracting the values present after those words?
body: <p><strong>Note:</strong> I asked a similar question but that was put "on hold" because i didn't provide my code (i guess). Now i have written my code also but i am facing some other problem.</p>
  
  <p>From my <code>.bench</code> file, i have to read the values written in brakects <code>()</code> which i managed to do. But the problem is that i have read the values in brakets after <code>INPUT</code>, <code>OUTPUT</code>, <code>NAND</code>.</p>
  
  <p><strong><code>.bench file</code></strong></p>
  
  <pre><code>INPUT(1)
  INPUT(2)
  INPUT(3)
  INPUT(6)
  INPUT(7)
  
  OUTPUT(22)
  OUTPUT(23)
  
  10 = NAND(1, 3)
  11 = NAND(3, 6)
  16 = NAND(2, 11)
  19 = NAND(11, 7)
  22 = NAND(10, 16)
  23 = NAND(16, 19)
  </code></pre>
  
  <p>So far, i have written the code to find the values inside brackets after <code>INPUT</code>, <code>OUTPUT</code>, and <code>NAND</code> but as it can be seen that i am repeating the similar lines of code again ana again. <strong>So, how can i generalize the same code to find vales after <code>OUTPUT</code>, <code>NAND</code> etc.</strong></p>
  
  <pre><code>int Circuit::readBenchFile(string filename) //read the benchfile and generate inputs, outputs and gates accordingly
  {
      //Reading the .bench file
      ifstream input_file;
      char * S = new char[filename.length() + 1];
      std::strcpy(S,filename.c_str());
  
      input_file.open(S);
      if(input_file.fail())
      {
          cout &lt;&lt; "Failed to open Bench file.\n";
          return 1;
      }
      ///////
  string line;
  string guard_str("#");
  string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND");
  
  while (getline( input_file, line ))  
  {
      std::size_t guard_found = line.find(guard_str);
      if (guard_found ==std::string::npos)
      {
          ///Input
          std::size_t found = line.find(input_str);
          if (found!=std::string::npos)
          {
              found = line.find_first_of('(', found + 1);
              //Getting our input name and printing it.
              string out = line.substr( found + 1, ( line.find_first_of(')', found) - found - 1) );
              std::cout &lt;&lt; out &lt;&lt; std::endl;
          }
  
          ///Output
          std::size_t found1 = line.find(output_str);
          if (found1!=std::string::npos)
          {
              found1 = line.find_first_of('(', found1 + 1);
              //Getting our input name and printing it.
              string out = line.substr( found1 + 1, ( line.find_first_of(')', found1) - found1 - 1) );
              std::cout &lt;&lt; out &lt;&lt; std::endl;
          }       
  
          ///NAND
          std::size_t found_2 = line.find(nand_str);
          if (found_2!=std::string::npos)
          {
              found_2 = line.find_first_of('(', found_2 + 1);
              //find first input
              string first_input = line.substr( found_2 + 1, ( line.find_first_of(',', found_2) - found_2 - 1) );
              //Second input
              found_2 = line.find_first_of(',', found_2 + 2);
              string second_input = line.substr( found_2 + 1, ( line.find_first_of(')', found_2) - found_2 - 1) );
  
              cout&lt;&lt;"\nInputs to NAND gate are:  "&lt;&lt;( first_input + string(" &amp; ") + second_input );
  
          }           
      }
  }           
  
  
  }
  </code></pre>