How to set working directory to "solutions directory" in C ++

I want to set the current directory to the solution directory / configuration name. How can I do it? can i use global variables in some way?

edit: I'm trying to read a file, and the current directory changes in the middle of the code. I want to change it.

+15
source share
4 answers

If your current directory changes, you should probably keep your working directory at startup in some variable that you can access later to set cwd there. At least thatโ€™s how I understand your question.

To get cwd this can help.

+2
source

In Visual Studio 2010:

  1. Go to the project properties (by clicking the project name in Solution Explorer, then select "Properties" from the pop-up menu).
  2. Then, in the "Configuration Properties / Debugging" section, set the working directory to $(SolutionDir)$(Configuration)\ .

Full list of available macros (at docs.microsoft.com): General macros for MSBuild commands and properties

+38
source

You can use the <direct.h> subsystem ( <direct.h> ) and access the functions

_getcwd()/_wgetcwd() Gets the current working directory
_chdir()/_wchdir() Sets the current working directory

If you need your cross-platform code, you can do the following:

 #ifdef _WIN32 # include <direct.h> # define getcwd _getcwd # define chdir _chrdir #else # include <unistd.h> #endif 

and use getcwd and chdir (without the main underscore).

+4
source

Have you tried using the environment variable $ (SolutionDir)?

Regarding this topic here .

In addition, we hope that the VS version does not matter, but this answer is based on the assumption that the platform is VS2005.

Hope this helps.

+3
source

All Articles