Reputation: 480
I am trying to have my program change directory (to where the user wishes to) but I am unable to navigate there and create a file? It appears that I am able to navigate there but when I get to the next system call it returns back to the current directory
Is there a way to set where my program cwd is pointing to?
std::string s1 = "cd " + userDirectory;
system(s1.c_str());
system("dir > test.txt");
Upvotes: 0
Views: 465
Reputation: 25690
current_path()
for C++ (std in C++17).Upvotes: 2
Reputation: 409404
The system
function starts a new command interpreter as a new process. And then runs the commands in that command interpreter. And as the cd
command is a built-in command it will only apply to that command interpreter process, not your process.
You have a couple of solutions you can try:
cd
and dir
and everything else) in to a script file that you run.Upvotes: 2
Reputation: 37578
On Windows you can change working directory using SetCurrentDirectoryW
function.
Upvotes: 0