title: Is it okay to open a file in the parameter of a function?
body: <p>I am checking if I did something correctly here in C++. Since recently coming out of Python, the way how things work in the language is a bit weird to me but I am getting used to it. </p>
  
  <p>The task was to:</p>
  
  <blockquote>
    <p>Step 1: Define a ​struct​ that can hold information about bank accounts. First:
    ■ Each instance will hold the name for the account and an account number. We have created an input file that has at least three accounts, called "accounts.txt":</p>
  </blockquote>
  
  <pre><code>"moe 6
  larry 28
  curly 42"
  </code></pre>
  
  <p>Read a file of account information, filling a vector of account objects. Define local variables corresponding to each data item. Loop through the file, reading into these variables. Clear the vector, reopen the file (call the open method), and​repeat the above, except using curly braced intializers to intialize the instance. Close the file and display all objects.</p>
  
  <blockquote>
    <p>Step 2: Define a ​class t​o hold accounts. Redo the above steps from task 1 that you did with the struct version. Use the ​same​ stream variable! Write getters to access the fields in the accounts when printing. Add an output operator for your class. First, Repeat the print loop using this output operator.Now​ make the output operator a friend, by adding a friend​prototype​ to your account class definition. Add a line in the output operator that prints the fields “directly”, i.e. without using the getters. The vector push_back method will be passed as temporary object defined as the argument to push_back. Repeat the filling of the vector using ​emplace_back ​and again display the contents.</p>
    
    <p>Step 3: Add ​transactions​ to your world.
    A transaction will be implemented as a class and have a field to indicate whether this is a deposit or a withdrawal and a field to say how much is being deposited or withdrawn. The account class will keep track of transactions applied to it. It will need a vector to store this history of transactions. It will also need a "balance" field to indicate how much is in the account. The account will have methods "deposit" and "withdrawal" which will be passed the amount and will add an appropriate transaction object to the history and modify the balance as needed. The output operator for the account will need to change so that it can display (you format it) the history, i.e. the transactions. When you finish, test this by reading in another file "transactions.txt" that has
    commands such as:</p>
  </blockquote>
  
  <pre><code>"Account moe 6
  Deposit 6 10
  Withdraw 6 100
  Account larry 28
  Withdraw 6 100
  Deposit 6 10
  Deposit 6 10
  Withdraw 6 5
  Account curly 42"
  </code></pre>
  
  <p>I am not sure if I opened the file <code>accounts.txt</code> correctly the way they wanted. I defined it inside the parameter of the function <code>readandDisplayUsingStruct</code>. Is it okay to do it this way? Would it seem like bad style of coding? Anyway, they were very specific in how I should perform this assignment with the steps they provided. It was more restrictive since they did not allow that I would conjure up my own way to do this.</p>
  
  <p>I provided a snippet of the code I've written below.</p>
  
  <pre><code>
  class AccountClass
  {
  private:
      string name;
      int accno;
      vector&lt;Transaction&gt; vHistory;
  public:
      AccountClass(string nm, int acno) :name(nm), accno(acno)
      {
  
      }
      string getName()
      {
          return name;
      }
  
      int getaccno()
      {
          return accno;
      }
  
      void addTransaction(Transaction t)
      {
          vHistory.push_back(t);
      }
  };
  
  void readandDisplayUsingStruct(string fileName= "accounts.txt")
  {
      ifstream fin;
  
      fin.open(fileName);
  
      if (!fin.is_open())
          return;
  
      string line;
  
      vector&lt;Account&gt; vAccounts;
  
      //Read each line from the file
      while (getline(fin, line))
      {
          stringstream ss(line);
          string name;
          int no;
          //Split the line into name and id using string stream
          ss &gt;&gt; name;
          ss &gt;&gt; no;
          Account acc{ name,no };
          vAccounts.push_back(acc);
      }
  
      cout &lt;&lt; "The Read Values are" &lt;&lt; endl;
  
      for (Account acc : vAccounts)
      {
          cout &lt;&lt; "Name : " &lt;&lt; acc.name &lt;&lt; "\n";
          cout &lt;&lt; "No : " &lt;&lt; acc.accno &lt;&lt; "\n";
      }
  
      //Clear the vector
      vAccounts.clear();
  
      fin.close();
  
      //Again do the same operation using local variables without Braces Initialisation.
      fin.open(fileName);
  
      if (!fin.is_open())
          return;
  
      string line1;
  
      vector&lt;Account&gt; vAccounts2;
  
      //Read each line from the file
      while (getline(fin, line1))
      {
          stringstream ss(line1);
          string name;
          int no;
          //Split the line into name and id using string stream
          ss &gt;&gt; name;
          ss &gt;&gt; no;
          Account acc;
          //Explicit initialisation
          acc.name = name;
          acc.accno = no;
          vAccounts2.push_back(acc);
      }
  
      std::cout &lt;&lt; "The Read Values are" &lt;&lt; endl;
  
      for (Account acc : vAccounts)
      {
          std::cout &lt;&lt; "Name : " &lt;&lt; acc.name &lt;&lt; "\n";
          std::cout &lt;&lt; "No : " &lt;&lt; acc.accno &lt;&lt; "\n";
      }
  
      fin.close();
  
  }
  
  </code></pre>