user4618119
user4618119

Reputation:

How to Change Working Directory C++11

I'm fairly new to C++ and I want to make a Program Launcher, All it does is launch applications for me based on text I input.

I all ready have the basic code for it, but I can't seem to find out how to change the working directory. I know you use chdir, but how Exactly.

Upvotes: 0

Views: 2993

Answers (2)

Stéphane
Stéphane

Reputation: 20390

Also see std::filesystem::current_path() which of course was added in C++17 as part of std::filesystem.

https://en.cppreference.com/w/cpp/filesystem/current_path

std::cout << "Old dir is " << std::filesystem::current_path() << std::endl;
std::filesystem::current_path("/foo/bar");
std::cout << "New dir is " << std::filesystem::current_path() << std::endl;

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283911

C++ still has very little standardized support for filesystem functions, in part because it runs on embedded devices which may not have working directories, or sometimes even directories or files at all. So we have to look to the OS API.

For POSIX, there is chdir() and getcwd().

For Windows, there is SetCurrentDirectory and GetCurrentDirectory, although if you want to deal with per-drive working directory, you will need to work with environment variables.

There is an example on MSDN titled Changing the Current Directory

Upvotes: 2

Related Questions