title: c++ how-to: import numeric variables from .txt or .csv file
body: <p>still being a beginner at C++, I cannot figure out how to use <code>fstream</code>.
  I want to assign values to a set of <code>double</code> variables in my program, from
  a <code>.txt</code> or a <code>.csv</code> file (<code>.csv</code> might be better for practical reasons.)</p>
  
  <p>Let's say that my <code>input_file.csv</code> looks like that:</p>
  
  <pre><code>10
  0
  20
  0.4
  0.1333382222
  0
  0.5
  10
  20
  0.76
  0.3
  0.1
  0.2
  </code></pre>
  
  <p>These values should be assigned to the following variables (first declared as equal to <code>0</code> ) in my code:</p>
  
  <pre><code>/// geometry
  double Dist=0; ///Distance between the 2
  
  double PosAi = 0;
  double PosAo = 0;
  double PosBi = 0;
  double PosBo = 0; ///positions i/o
  
  /// densities
  
  double iDA=0;
  double oDA=0;
  double iDAtop=0;
  double oDAtop=0; /// Left
  
  double iDB=0;
  double oDB=0;
  double iDBtop=0;
  double oDBtop=0; /// Right
  </code></pre>
  
  <p>I want to read the values of <code>input_file.csv</code> and assign them to my variables, so that if I type:</p>
  
  <pre><code>cout&lt;&lt;Dist&lt;&lt;" "&lt;&lt;PosAi&lt;&lt;" "&lt;&lt;PosAo&lt;&lt;" "&lt;&lt;
  </code></pre>
  
  <p>...........etc. <code>;</code></p>
  
  <p>I get the following list on the console:</p>
  
  <pre><code>10 0 20 0.4 0.1333382222 0 0.5 10 20 0.76 0.3 0.1 0.2
  </code></pre>
  
  <p>But I don't know how to use fsteam for that, could you please help a bit?
  Thanks!</p>
  
  <hr>
  
  <p>Ok here's the answer if ever some beginner like me gets the same problem:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  /// geometry
  
  double Dist=0; ///Distance between the 2
  double PosAi = 0;
  double PosAo = 0;
  double PosBi = 0;
  double PosBo = 0; ///positions i/o
  
  /// densities
  
  double iDA=0;
  double oDA=0;
  double iDAtop=0;
  double oDAtop=0; /// Left
  double iDB=0;
  double oDB=0;
  double iDBtop=0;
  double oDBtop=0; /// Right
  
  int main()
  {
  ifstream ifs ("input.csv");
  if (!ifs)
      // process error
  ifs &gt;&gt; Dist;
  ifs &gt;&gt; PosAi;
  ifs &gt;&gt; PosAo;
  ifs &gt;&gt; PosBi;
  ifs &gt;&gt; PosBo;
  ifs &gt;&gt; iDA;
  ifs &gt;&gt; oDA;
  ifs &gt;&gt; iDAtop;
  ifs &gt;&gt; oDAtop;
  ifs &gt;&gt; iDB;
  ifs &gt;&gt; oDB;
  ifs &gt;&gt; iDBtop;
  ifs &gt;&gt; oDBtop;
  
      // print variables
  
      cout &lt;&lt; Dist &lt;&lt; " " &lt;&lt; PosAi &lt;&lt; " " &lt;&lt; PosAo &lt;&lt; " " &lt;&lt; PosBi &lt;&lt; " " &lt;&lt; PosBo &lt;&lt; " " &lt;&lt; iDA &lt;&lt; " " &lt;&lt; oDA &lt;&lt; " " &lt;&lt; iDAtop &lt;&lt; " " &lt;&lt; oDAtop &lt;&lt; " " &lt;&lt; iDB &lt;&lt; " " &lt;&lt; oDB &lt;&lt; " " &lt;&lt; iDBtop &lt;&lt; " " &lt;&lt; oDBtop &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Thanks</p>