title: Read complex numbers (a+bi) from text file in C++
body: <p>i want to read an array of complex numbers (in <code>a+bi</code> form). There are several suggestions I found on the internet, however those methods only result the <code>real part</code>, while the <code>imaginary part</code> is always <code>0</code>. And infact the <code>real part</code> of the next number is the <code>imaginary</code> of the previous number.</p>
  
  <p>For example, I have an text file as follow:</p>
  
  <pre><code>2+4i
  1+5i
  7+6i
  </code></pre>
  
  <p>And here is a suggestion for reading complex data</p>
  
  <pre><code>int nRow = 3;
  std::complex&lt;int&gt; c;
  std::ifstream fin("test.txt");
  std::string line;
  std::vector&lt;std::complex&lt;int&gt; &gt; vec;
  vec.reserve(nRow);
  while (std::getline(fin, line))
  {
      std::stringstream stream(line);
      while (stream &gt;&gt; c)
      {
          vec.push_back(c);
      }
  }
  
  for (int i = 0; i &lt; nRow; i++){
      cout &lt;&lt; vec[i].real() &lt;&lt; "\t" &lt;&lt; vec[i].imag() &lt;&lt; endl;
  }
  
  while (1);
  return 0;
  </code></pre>
  
  <p>And the output result is:</p>
  
  <pre><code>2   0
  4   0
  1   0
  </code></pre>
  
  <p>Is there a proper way to read <code>a+bi</code> complex numbers from text file? Or do I have to read the data as a string, and then process the string to extract and convert it back to complex numer?</p>
  
  <p>Thanks !</p>