title: C++ - How do you get the current directory of the C++ executable?
body: <p>I am creating a game engine in C++, and I had started adding support for loading files. I created a load file method that looks like this:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;fstream&gt;
  
  std::string read_file(const char* filepath) {
  
          FILE* file = fopen(filepath, "rt");
          if(file != NULL) {
              fseek(file, 0, SEEK_END);
          } else {
              std::string error_message = std::string("[ERROR]: Failed to load file ");
              error_message.append(filepath);
              return error_message;
          }
          unsigned long length = ftell(file);
          char* data = new char[length + 1];
          memset(data, 0, length + 1);
          fseek(file, 0, SEEK_SET);
          fread(data, 1, length, file);
          fclose(file);
  
          std::string result(data);
          delete[] data;
  
          return result;
  
      }
  </code></pre>
  
  <p>To test this function, I decided to try to load a "test.txt" file, but this will not work unless I include the full path, e.g. </p>
  
  <pre><code>"/Users/(Username)/.../(Executable Directory)/text.txt"
  </code></pre>
  
  <p>instead of just</p>
  
  <pre><code>"test.txt"
  </code></pre>
  
  <p>So, to fix this, I need a way to get the full path of the directory that the executable file is in, and then append the file name onto the end of the path, and load that in my load file method.</p>
  
  <p>Anyone know how to get the path?</p>