title: How to read an array of complex numbers from a text file in C++
body: <p>As a learner in c++, I decided to play with complex numbers, using the standard library. Now I need to read and write an array of complex from/to text files. This works simply for writing, without supplemental tricks : </p>
  
  <pre><code>void dump(const char *filename){
    ofstream result;
    result.open (filename);
    for(int k=0;k&lt;15;k++){
      result&lt;&lt; outputs[k] &lt;&lt;endl;
    }
    result.close();
  }
  </code></pre>
  
  <p>The data are parenthesized and written line by line looking like : (real,im)...</p>
  
  <p>Now, I guess reading (and loading an array of complex) should be as trivial as reading. However, despite my research, I have not found the right way to do that.</p>
  
  <p>My first attempt was naive :</p>
  
  <pre><code>void readfile(const char *filename){
    string line;
    ifstream myfile (filename);
    if (myfile.is_open())
    {
      int k=0;
      while ( getline (myfile,line) ){
        k++;
        cout &lt;&lt; line &lt;&lt; endl;
        inputs[k]= (complex&lt;float&gt;) line; //naive !
      }
      myfile.close();
    }
    else cout &lt;&lt; "Unable to open file"; 
  }
  </code></pre>
  
  <p>Is there a way to do that simply (without a string parser ) ?</p>