Mangling An Applications Path

I was looking to get rid of using some environment variables in the app I do at work for several reasons. One, you can't grab fresh environment variables on the fly (or at least I can't) so if anything needs to be changed, you have to do a hard kill and restart the program. Two is that requiring some obscure, application specific environment variables seems a little silly to me, it's excessive and adds bothersome configuration.

What I really needed was a way to open a config file that always resides in the same directory as the application. To do that I had to take the execution path (e.g. argv[0]) and chop it up a bit so that I can always get to that directory. I'm sure there is a better way to do this, and it wouldn't work if you had the application in your $PATH, because then argv[0] would just be the executable name. Regardless of the caveats, it works nicely for me and makes me feel better about myself now that I can frag the environment variables.


using namespace std;

int main (int argc, char* argv[]) {

  cout << "File: " << argv[0] << endl;

  size_t found;
  string str = argv[0];

  if (found!=string::npos)

  cout << "Trimmed: " << str << endl;
  fstream filestr;

  str += "test.conf";
  cout << "To Conf: " << str << endl; (str.c_str(), fstream::in);
  string temp;

  cout << temp << endl;


  return 0;