Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the working directory to the "solution directory" in c++?

I want to set the current directory to the solution directory/configuration name. How do I do that? Can I use the global variables somehow?

I am trying to read a file and the current directory changes in the middle of the code. I want to change it back.

like image 959
lital maatuk Avatar asked Jan 27 '11 10:01

lital maatuk


People also ask

How do I change the working directory of a file?

Answer: Use the cd Command The current working directory is the directory or folder where you are currently working. You can use the cd (change directory) command to change the current working directory or move around the file system. This command will work in all Linux distribution.

How do I change the working directory in Visual Studio?

In Visual Studio 2010: Go to the project properties (rigth click on the project name in the Solution Explorer, then Properties on the pop up menu). Then, under Configuration Properties / Debugging, set Working Directory to $(SolutionDir)$(Configuration)\ .

What is $( ProjectDir?

$(ProjectDir)The directory of the project (defined as drive + path); includes the trailing backslash '\'.


3 Answers

In Visual Studio 2010:

  1. Go to the project properties (rigth click on the project name in the Solution Explorer, then Properties on the pop up menu).
  2. Then, under Configuration Properties / Debugging, set Working Directory to $(SolutionDir)$(Configuration)\.

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

like image 171
rturrado Avatar answered Sep 20 '22 06:09

rturrado


You can use the posix 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 code to be cross platform, 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 (w/o the leading underscore).

like image 21
KitsuneYMG Avatar answered Sep 19 '22 06:09

KitsuneYMG


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

With reference to this thread here.

Also, hopefully, the version of VS does not matter, but this answer is furnished based on the assumption that the platform is VS2005.

like image 28
evandrix Avatar answered Sep 19 '22 06:09

evandrix