I am using _getcwd() to get the full path of the current working directory but I am having a strange problem with this:

When I run the program from within Visual Studio _getcwd returns "F:\...\ProgramName\bin" to which I append "\Debug\config.txt" to give the path of a text file. However, when I click the exe the program (located in F:\...\ProgramName\bin\Debug) does not work because _getcwd returns "F:\...\ProgramName\bin\Debug". Here's the code I am using

char filepath_config[_MAX_PATH];
        _getcwd(filepath_config, _MAX_PATH);

	strcat_s(filepath_config, "\\Debug\\config.txt");
	ifstream config_in(filepath_config);

Has anyone any idea's why this would happen? I would have expected _getcwd() to return the filepath "F:\...\ProgramName\bin\Debug" always.

Thanks in advance.

Edited 5 Years Ago by v_janssens: Forgot to add code

I know that running a program through an environment such as Visual C++ or CodeBlocks will automatically set the working directory to whatever you choose, if you set it up correctly. Your IDE is probably set up to always run your Debug program straight from the /bin/ folder, but when you run it directly as an EXE, it's running it from the folder it's in.

I've had a similar problem in the past which caused me many headaches with my programs. It turns out that clicking and dragging a file over an EXE in Windows will change the working directory, and this prevented my program from finding vital files in the same directory. I eventually had to write a function that would force it to load files from the directory it's in.

I'm glad to hear I'm not alone Tumlee. Is there an alternative to _getcwd() that is less troublesome?

After toying around with this for the day I have decided against using _getcwd() and have opted for the following approach to getting the containing folder for executable:

char filepath_config[_MAX_PATH];
GetModuleFileName(NULL, filepath_config, _MAX_PATH);
string::size_type pos = string(filepath_config).find_last_of( "\\");
filepath_config[pos] = '\0';   
strcat_s(filepath_config, "\\config.txt");

That is very similar to how I have solved my problem, the only difference is that I used C-style strings and I got the directory of the executable from argv[0], and I avoided using Windows-specific functions because I had to cross-compile it eventually. What I ended up with was this, if you're curious:

char* get_exe_dir(void)
{
   static char* exedir = NULL;
   if(!exedir)   //Generate the exedir by looking at argv[0]
   {
   //Search for the last directory seperator.
      int slashpos = -1;
      for(int i = 0; global_argv[0][i] != '\0'; i++)
      {
         if(global_argv[0][i] == '/' || global_argv[0][i] == '\\')
            slashpos = i;
      }
   //This should work even if slashpos == -1
      exedir = new char[slashpos+2];
      memcpy(exedir, global_argv[0], slashpos+1);
      exedir[slashpos+1] = '\0';

   //For consistency, change all backslashes to forward-slashes.
   //All operating systems, including Windows, support forward-slash as a dir separator.
      for(int i = 0; exedir[i] != '\0'; i++)
         if(exedir[i] == '\\')   exedir[i] = '/';
   }
   return exedir;
}
This question has already been answered. Start a new discussion instead.