title: how can i close files after open them in a function in c++
body: <p>I work on using extensible hash to find the query FASTER.
  my code is this steps:
  1)read the main text file ( hudge file 4 GiB)
  the file is some thing like this :</p>
  
  <pre><code>12435 alex romero
  13452 jack robert
  13485 marya car
  45132 gun tribble
  ...
  </code></pre>
  
  <p>the user want to know that for example the key 12435 is related to what ?(answer:alex romero)</p>
  
  <p>2)create a hash table for the keys in the file (i means 12435,13452,13485,...)
  and i save this tables dynamically in hard disk in some text files named:0.txt,1.txt,2.txt and ....</p>
  
  <p>3)when the user get query to the program then the program must calculate the hash function on its value and find the file that must be read then it is faster to find the result.</p>
  
  <p>i have a function:</p>
  
  <pre><code>#define LIMIT 7
  void writeInFile(int key , const char* charPos ){
      int remainder = key%(LIMIT*LIMIT);
  
      string myFileName;
      ostringstream convert;
      convert &lt;&lt; remainder ;
  
      myFileName = convert.str();
      myFileName += ".txt";
  
      FILE *my_file;
      my_file = fopen(myFileName.c_str() ,"a");
  
      fputs("\n" ,my_file);
      fputs(charPos , my_file);
      //fclose(my_file);
  }
  </code></pre>
  
  <p>i wondered that when i use <code>fclose</code> then the speed of the program will reduced !!!
  then i dont use it at the end of the function but a problem that is when i use this function many times i can't close them then i cant get access to the files.
  i want to create a "list" of FILEs that i can send refrence of them to the function like: <code>FILE &amp;* myFiles[]</code> or <code>FILE &amp;** myFiles</code> as 3th parameter that function gets...
  but i see the errors .i dont know how is its syntax of this.i means some syntax like:</p>
  
  <pre><code>void writeInFile(int key , const char* charPos , FILE &amp;*myFiles[] ) // this makes error
  </code></pre>
  
  <p>the other method that i think is that can i close those files that now I can't access to them ? or can i change my code that cause this ?</p>
  
  <p>update:this is my full code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;limits&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;vector&gt;
  
  #define LIMIT 7
  
  using namespace std;
  
  void writeInFile(int key , const char* charPos ){
      int remainder = key%(LIMIT*LIMIT);
  
      string myFileName;
      ostringstream convert;
      convert &lt;&lt; remainder ;
  
      myFileName = convert.str();
      myFileName += ".txt";
  
      FILE *my_file;
      my_file = fopen(myFileName.c_str() ,"a");
  
      fputs("\n" ,my_file);
      fputs(charPos ,my_file);
      //fclose(my_file);
  
  }
  
  int main(){
      string fileName;
      cout &lt;&lt; "hello, please inter your file destination : " ;
      cin &gt;&gt; fileName;
      ifstream myFile ;
      myFile.open(fileName.c_str() ,ifstream::in |ifstream::binary);
      cout &lt;&lt; "building the hash,please wait";
      string havij;//:D this is an unusable variable in this section :))
      int current;
      int index;
      int isCout=0;
      char buffer [10];
  
      //FILE *my_file[49];
      while(!myFile.eof()){
  
          cout &lt;&lt; isCout &lt;&lt; endl;
          isCout++;
          index = myFile.tellg();
  
  
          itoa(index , buffer ,10);
          //cout &lt;&lt; buffer &lt;&lt; endl;
          myFile &gt;&gt; current;
          writeInFile(current ,buffer);
          getline(myFile,havij);
      }
      myFile.close();
      fstream test;
      //for(int i =0 ; i&lt;LIMIT*LIMIT-1 ; i++){
      //  fclose(my_file[i]);
      //}
      cout &lt;&lt; endl &lt;&lt; "static extensible hash structure builded please inter your query : " ;
      int query;
      cin &gt;&gt; query;
      int remainder = query%(LIMIT*LIMIT);
  
      string myFileName;
      ostringstream convert;
      convert &lt;&lt; remainder ;
  
      myFileName = convert.str();
      myFileName += ".txt";
  
      ifstream myFile2;
      //myFile2 is now the files that create by program like : 12.txt ,25.txt ,....
      myFile2.open(myFileName.c_str() , ifstream::in | ifstream::binary);
      ifstream mainFile;
      mainFile.open(fileName.c_str(), ifstream::in | ifstream::binary);
      int position;
      string wanted;
      int tester;
      while(!myFile2.eof()){
          myFile2 &gt;&gt; position;
  
          mainFile.seekg(position ,ios::beg);
          mainFile &gt;&gt; tester;
          if (tester == query ){
              getline(mainFile ,wanted);
              cout &lt;&lt; "the result of the key " &lt;&lt; tester &lt;&lt; " is  " &lt;&lt; wanted &lt;&lt; endl;
          }
      }
      return 0;
  }
  </code></pre>