title: Reading a text file with C++ to read in the value that follows a specific key word
body: <p>I am attempting to write a generic text file reader in C++17 that will search a text file, line by line, for a specific key word and the code should read in the data point that follows that key word.  I am writing it with a template function within a class so that it can read in any data type.  In this example lets say that I have the following text file titled <code>test.txt</code>.</p>
  
  <pre><code>- test.txt file
  integer key: 4
  Float key: 6.04876
  Double Key: 12.356554545476756565
  String Key: test
  </code></pre>
  
  <p>The header file containing the template class looks like this.  In this case the <code>ReadTextFile</code> class inherits another class to assist with 
  checking the file status.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;sstream&gt;
  
  #ifndef read_files_hpp
  #define read_files_hpp
  
  class FileStatus
  {
  public:
      template &lt;class container&gt;
      bool file_exists(const container&amp; file_name);
      template &lt;class file_container&gt;
      bool is_file_open(const file_container&amp; file);
  };
  template &lt;class container&gt;
  bool FileStatus::file_exists(const container&amp; file_name)
      std::ifstream ifile(file_name.c_str());
      return (bool)ifile;
  }
  
  template &lt;class file_container&gt;
  bool FileStatus::is_file_open(const file_container&amp; file)
  {
      return file.is_open();
  }
  // ----------------------------------------------------------------
  
  
  class ReadTextFile : public FileStatus
  {
  public:
      template &lt;class dtype, class container1, class container2&gt;
      dtype read_key_words(const container1&amp; file_name, const
                           container2&amp; key_word);
  };
  
  template &lt;class dtype, class container1, class container2&gt;
  dtype ReadTextFile::read_key_words(const container1&amp; file_name,
                                     const container2&amp; key_word)
  {
      int j = 3;
      if (bool i = file_exists(file_name) != 1) {
          std::cout &lt;&lt; "FATAL ERROR: " &lt;&lt; file_name &lt;&lt;
          " not found" &lt;&lt; std::endl;
          return -1;
      }
      std::ifstream file(file_name);
      if (is_file_open(file))
      {
          std::string line;
          while (std::getline(file, line))
          {
              std::cout &lt;&lt; line.c_str() &lt;&lt; std::endl;
          }
      }
      return j;
  }
  // ================================================================
  // ================================================================
  #endif
  </code></pre>
  
  <p>The calling program <code>main.cpp</code> looks like this;</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include "read_files.hpp"
  int main() {
      std::string file_name("../data/unit_test/keys.txt");
      std::string key_word("integer key:");
      int j;
      j = txt_file.read_key_words&lt;int&gt;(file_name, key_word);
  }
  </code></pre>
  
  <p>In this test case the class is being instantiated as type <code>int</code> so until the program is completely written I am returning the variable <code>int j = 3;</code> from the function <code>read_key_words()</code>.  At present the code can read the file <code>test.txt</code> which is in the same directory and correctly read in each line.  I would like the code to parse each line to recognize if the phrase <code>integer key:</code> is present and then read the variable that follows it, as the data type that the function is instantiated for, which in this case is an integer.  How can I make this happen within the code? </p>