title: C++: Read from text file and separate into variable
body: <p>I have this in a text file:</p>
  
  <pre><code>John 20 30 40
  mike 30 20 10
  </code></pre>
  
  <p>How do i read from the text file and separate them into variable name, var1, var2, var3.
  This is my attempt, seems it doesnt work. Help please.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  int main () {
    string name,result;
    int number1;
    ifstream myfile ("marks.txt");
    if (myfile.is_open())
    {
      while ( !myfile.eof() )
      {
        getline (myfile,name,'\t');
        getline (myfile,var1,'\t');
        getline (myfile,var2,'\t');
        getline (myfile,var3,'\t');
        cout &lt;&lt; name &lt;&lt; var1 &lt;&lt; var2 &lt;&lt; var3;
  
  
      }
  
      myfile.close();
  
    }
  
    else cout &lt;&lt; "Unable to open file";
  
    return 0;
  }
  </code></pre>
  
  <p>EDIT 1:</p>
  
  <p>Nocturne Suggestion:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  
  int main()
  {
      ifstream inputFile("marks.txt");
      string line;
  
      while (getline(inputFile, line))
      {
          istringstream ss(line);
  
          string name;
          int var1, var2, var3;
  
          ss &gt;&gt; name &gt;&gt; var1 &gt;&gt; var2 &gt;&gt; var3;
          cout &lt;&lt; name &lt;&lt; var1 &lt;&lt; var2 &lt;&lt; var3 &lt;&lt; endl &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>John203040
  
  mike302010
  
  302010
  </code></pre>
  
  <p>Why another 302010???</p>