title: Array of fstream in C++
body: <p>I'm writing an algorithm with NTL library(it's a C++ library). Now I have N files to edit and I have to create/open them at the same time. I try to dynamically allocate space for the N file pointers but there's something wrong with the code. The code snippet is shown below:</p>
  
  <pre><code>P1 = (char **)calloc(n+1, sizeof(char *));//P1 is used to save file names
  for(i=1; i&lt;=n; i++)
  {
      P1[i]=(char *)calloc(20, sizeof(char ));
      sprintf(P1[i],"P1_%d.txt",i);
  }
  ifstream *fin = (ifstream *)calloc(n+1, sizeof(ifstream));
  for(i=1; i&lt;=n; i++)
  {
      fin[i].open(P[i]);
  }
  </code></pre>
  
  <p>When the program runs in linux, it tells me there is a Segmentation Fault. </p>
  
  <p>Since N is not larger than 200, when I try to use</p>
  
  <pre><code>ifstream fin[200]
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>ifstream *fin = (ifstream *)calloc(n+1, sizeof(ifstream));
  </code></pre>
  
  <p>the program runs. </p>
  
  <p>I just learned C but not C++, and I don't really know how the <code>fstream</code> class works. Could you tell me if there is some better ways to open N files at the same time?</p>