title: Reading only the formatted data in a file that has formatted and unformatted data using c++
body: <p>I have a data file with an unknown amount of unformatted, not needed data at the start and end of the file. But, in the middle, the data is precisely formatted and the first column will always start with one of a couple keywords. I want to skip to this part and read in that data, assigning each column to a variable. This would be simple if there wasn't the start and end "garbage" text.</p>
  
  <p>Here is simple example problem. In my real code, each variable is part of a structure. I do not think this will matter, but mention it just in case...</p>
  
  <p>here is my text file, I want all lines that start with keyword, and I want all columns assigned to variables</p>
  
  <hr>
  
  <pre><code>REMARK: this should be simpler
  REMARK: yes, it should
  REMARK: it is simple, you just don't see it yet
  Comment that doesn't start with REMARK
  keyword aaa 1 bbb 1 1.2555  O
  keyword aaa 1 bbb 2 2.2555  H
  keyword aaa 1 bbb 3 3.2555  C
  keyword aaa 1 bbb 4 4.2555  C
  END
  Arbitrary garbage texts
  </code></pre>
  
  <hr>
  
  <p>if there were no random comments, I could use</p>
  
  <pre><code>int main{
      string filename = "textfile.pdb";
      string name1,name2,name3;
      int int1, int2;
      double number;
  
      ifstream inFile;
      inFile.open(filename.c_str());
  
      while (inFile.good())
      {
          inFile &gt;&gt; keyword &gt;&gt; name1 &gt;&gt;  
          int1&gt;&gt;name2&gt;&gt;int2&gt;&gt;number&gt;&gt;name3;
      }
      inFile.close();
  }
  </code></pre>
  
  <p>I tried getting around this by using </p>
  
  <pre><code>while (getline(inFile,line))
  </code></pre>
  
  <p>This method lets me look at the line, and check if it has "keyword" in it. but then I couldn't use the convenient formatted input of the first method. I need to parse the string, which seems tricky in c++.I tried using sscanf but it complained about str to char.</p>
  
  <p>The first method is nicer, I just don't know how to implement a check to only read in the line to the variables, if the line is a formatted one.</p>