title: How to read a file line by line and access the data separated by space using c++
body: <p>The file is a .dat file and I was able to read into the file but I can't find a way to access the data separated by the space so that I can calculate the year mark for the subject as a percentage.</p>
  <p>The first field in each line represents the subject code.</p>
  <p>followed by the percentage assignment 1 contributes towards the year mark,</p>
  <p>followed by the students mark they got on assignment 1,</p>
  <p>followed the percentage assignment 2 contributes towards the year mark,</p>
  <p>followed the students' mark they got on assignment 2.</p>
  <p>To view, the file data, Open the image link at the end of the code.</p>
  <p>My code so far:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  
  using namespace std;
  int main()
  {
      string line_;
  
  
      ifstream yearmark;
      yearmark.open (&quot;assignments.dat&quot;);
  
      if (yearmark.is_open())
      {
          while (getline (yearmark,line_))
          {
              cout&lt;&lt; line_&lt;&lt;'\n';
          }
          yearmark.close();
      }
      else
      {
          cout&lt;&lt; &quot;file not open&quot;&lt;&lt; endl;
  
      }
  
      cout&lt;&lt; endl;
  
  
      ofstream file (&quot;yearmarks.dat&quot;);
  
      file.close();
  
  return 0;
  
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/ykZXT.jpg" rel="nofollow noreferrer">.dat file image</a></p>