title: how to change the working directory to the location of the program
body: <p>I want to use c++ to open a file on Mac OS. </p>
  
  <p>If I run the program under Xcode, the working directory is the same with the program, which is fine. However, if I try to run the program in terminal, the working directory is alway "Users/username". Do you know how to change the working directory to the location of the program? </p>
  
  <p>Here is the sample code: </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  int main (int argc, const char * argv[])
  {
      char * dir = getcwd(NULL, 0); 
      cout &lt;&lt; "Current dir: " &lt;&lt; dir &lt;&lt; endl;
  
      ifstream fin("hi.txt");
      if (fin.is_open()) cout &lt;&lt; "File is Open" &lt;&lt; endl;
      else cout &lt;&lt; "File is not open" &lt;&lt; endl;    
      fin.close();
      return 0;
  }
  </code></pre>