title: Reading and writing double precision from/to files
body: <p>I am trying to write double arrays into files and read them again. Below is my code, but there is something I am missing. It sounds silly but I cannot get it right.   </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(){
  
    int i,j,k;
    int N = 10;
  
    double* readIn = new double[N];
    double* ref = new double[N];
    FILE* ptr1, *ptr2;
  
    ptr1 = fopen("output.txt","w");
    //write out
    for (i = 0; i &lt; N;i++){
      ref[i] = (double)i;
      fprintf(ptr1,"%g\n",(double)i);
    }
    fclose(ptr1);
    //read file
    ptr2 = fopen("output.txt","r+");
    //read in
    for(i = 0;i &lt; N;i++)
      fscanf(ptr2, "%g", &amp;readIn[i]); 
  
  
    fclose(ptr2);
  
    for(i = 0;i&lt;N;i++)
      if(ref[i] != readIn[i]){
        printf("Error:  %g   %g\n",ref[i], readIn[i]);
      }
  
    return 0;
  
  }
  </code></pre>