title: How often should one open/close fstream object c++
body: <p>so the write elm and getfileID functions require the cursor pos in the file 
  (write elm appends to the end, getFileID prints lines first to last)     </p>
  
  <pre><code>    #ifndef file_operations_header
      #define file_operations_header
      #include &lt;string&gt;
      #include &lt;iostream&gt;
      #include &lt;vector&gt;
      #include &lt;fstream&gt;
      #include "First_classes_header.h"
  
  
      class fileOPerations
      {
      private:
          string line;
          fstream f_myFileOut;
      public:
          fileOPerations();
  
          void closeFile()
          {
              f_myFileOut.close();
          }
  
          int getFileID()
          {
              int counter = 0;
              if (f_myFileOut.is_open())
              {
                  while(f_myFileOut.good()){
                      getline(f_myFileOut,line);
                      ++counter;
                      cout &lt;&lt; line &lt;&lt; endl;
                      }
              }f_myFileOut.close();
              return counter;
          }
  
  
  
          int writeElm(makeVector&amp; mV,int i)
          {
              f_myFileOut.open("file.txt",ios::out|ios::app|ios::ate);
              if (f_myFileOut.is_open())
              {
                  f_myFileOut &lt;&lt; mV.str_vector[i].counter &lt;&lt; "\t";
                  f_myFileOut &lt;&lt; mV.str_vector[i].name &lt;&lt; endl;
              }
              else{
                  cout &lt;&lt; "can't open file." &lt;&lt; endl;
              }
              return 0;
          }
  
  
          friend class makeVector;
      };
  
      fileOPerations::fileOPerations():f_myFileOut("file.txt",ios::out|ios::app|ios::in){}
  
      #endif // file_operations_header
  </code></pre>
  
  <p>and the call to getFileID in my main doesn't print anything because writeElm()
  set the cursor pos to the end of the file.</p>
  
  <pre><code>        #include &lt;iostream&gt;
          #include &lt;string.h&gt;
          #include &lt;vector&gt;
          #include "First_classes_header.h"
          #include "file_operations.h"
          using namespace std;
  
  
  
          int main()
          {
              fileOPerations fpObject;
              makeVector vecObject;
  
              int fileID = fpObject.getFileID();
  
              while(true){
                  IDgenerator();
  
                  int genID = IDgenerator::GetID();
                  int currentID = fileID + genID;
                  string workingName = nameGetter::setName();
  
  
                  vecObject.vecSetter(currentID,workingName);
                  fpObject.writeElm(vecObject, currentID); // error within this function
  
                  fpObject.getFileID();
  
              }fpObject.closeFile();
  
  
              return 0;
  
          }
  </code></pre>
  
  <p>Is it safe/effecient/effective to call f_myFileOut.open() with different parameters
  in each separate function? </p>
  
  <pre><code>        int getFileID()
          {
              f_myFileOut.open(("file.txt",ios::out|ios::app|ios::in)
  
              int counter = 0;
              ...
  </code></pre>
  
  <p>...</p>
  
  <pre><code>    int writeElm(makeVector&amp; mV,int i)
      {
          f_myFileOut.open("file.txt",ios::out|ios::app|ios::ate);
  </code></pre>
  
  <p>Or should I set the cursor pos manually? </p>