title: Reading Data from Text File. I need to do two things, first I need to read the number of points
body: <p>I have a text file arranged as:</p>
  
  <pre><code>Diam
  &lt;D&gt;          4.2      
  05:21:26 02:Apr:2012
  Point 1
  &lt;X&gt;       2   
  &lt;Y&gt;       5
  &lt;Z&gt;       6  
  Point 2
  &lt;X&gt;       4   
  &lt;Y&gt;       2
  &lt;Z&gt;       0
  Point 3
  &lt;X&gt;       4 
  &lt;Y&gt;       1 
  &lt;Z&gt;       2 
  End
  End
  </code></pre>
  
  <p>I need to do two things, first I need to read the number of points, so basically read how many lines start with Point, for this I wrote:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main()
  {
      //count number of data points in file (only count lines starting with P)
      const char FileName[] = "myfile.txt";
      int num = 0.;
      string line;
      ifstream file;
      file.open (FileName);
      while(file.eof() == false)
      {
          getline(file, line);
          if (line[0]=='P')  num++;
      }
      file.close();
  
      return 0;
  }
  </code></pre>
  
  <p>This gives me the correct number of points (although improvements in the code are welcome), and secondly I need to read the x, y, and z coordinates and disregard everything else. 
  Not sure how to go about this, any suggestions?</p>
  
  <p>Thanks in advance!</p>
  
  <p>EDIT:</p>
  
  <p>Thanks everybody for great ideas and answers! Captain Obvlious provided a great answer, and I want so see if it can be adapted a bit.</p>
  
  <p>So, turns out the .txt file will have the format:</p>
  
  <pre><code>Diam
  &lt;D&gt;          4.2      5    6    4   2
  05:21:26 02:Apr:2012
  Point 1
  &lt;X&gt;       2   5    6    4   2
  &lt;Y&gt;       5   4    4    8   3
  &lt;Z&gt;       6   7    6    0   2
  Point 2
  &lt;X&gt;       4   2    6    4   2
  &lt;Y&gt;       2   3    5    8   4
  &lt;Z&gt;       0   7    6    3   2
  Point 3
  &lt;X&gt;       4   0    6    2   2
  &lt;Y&gt;       1   5    6    7   4
  &lt;Z&gt;       2   0    6    5   3
  End
  End
  </code></pre>
  
  <p>Again, I only need the first column of values, everything else after that can be discarded.
  I used <code>int</code> for simplicity but the numbers will be <code>double</code>.
  After the reading I am trying to place the values in an <a href="http://arma.sourceforge.net/" rel="nofollow">armadillo</a> matrix, so my matrix looks like:</p>
  
  <pre><code>cout &lt;&lt; matrix &lt;&lt; endl;
  2 5 6
  4 2 0
  4 1 2 
  </code></pre>
  
  <p>So, I modified Captain Obvlious' solution to place the data in the matrix:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cctype&gt;
  #include &lt;sstream&gt;
  #include &lt;stdexcept&gt;
  #include &lt;armadillo&gt;
  
  using namespace std;
  using namespace arma;
  
  int main()
  {
      mat matrix(3,3);  //here is where I set the size of the matrix, 
                        //different files have different number of points,
                        //that's why I use my counting points code to set the size.
      const char FileName[] = "myfile.txt";
      string line;
      ifstream file;
      file.open (FileName);
  
      for(;;)
      {
          getline(file, line);
          if( file.eof() ) {
              break;
          }
  
          if (line.size() &amp;&amp; line[0]=='P')
          {
              // Change "int" to "double" if necessary.
              struct { double  x, y, z; } data;
  
              for(int i = 0; i &lt; 3; i++)
              {
                  getline(file, line);
                  if(line.size() &gt; 3 &amp;&amp; line[0] == '&lt;' &amp;&amp; line[2] == '&gt;')
                  {
                      string::value_type pointElement = line[1];
  
                      // skip spaces and process data here to get the value
                      string::size_type offset = line.find(' ');
                      if(string::npos == offset)
                      {
                          throw invalid_argument("Invalid data format");
                      }
  
                      stringstream sline(line.substr(offset));
                      int value;
                      if(!(sline &gt;&gt; value))
                      {
                          throw invalid_argument("invalid data format");
                      }
  
  
                      switch(pointElement)
                      {
                      case 'X':   data.x = value; break;
                      case 'Y':   data.y = value; break;
                      case 'Z':   data.z = value; break;
                      default:
                          // error in data format
                          throw invalid_argument("invalid data format");
                      }
                  }
                  else
                  {
                      // error in data format
                      throw invalid_argument("invalid data format");
                  }
              }
  
              // Do something with the values in data
              cout
                  &lt;&lt; "point[x=" &lt;&lt; data.x
                  &lt;&lt; ", y=" &lt;&lt; data.y
                  &lt;&lt; ", z=" &lt;&lt; data.z
                  &lt;&lt; "]" &lt;&lt; endl;
              // place data in matrix
              // need to loop over k where k is the row of the matrix
              matrix(k,0) = data.x; // format is matrix(rows,columns)
              matrix(k,1) = data.y;
              matrix(k,2) = data.z;
          }
      }
      file.close();
  
      return 0;
  }
  </code></pre>
  
  <p>Not sure where to place the loop over k that sets the row of the matrix to allocate the values.
  Sorry for the super long post, and thank you in advance for any help. This is pretty useful stuff to have around for future reference in my opinion.</p>