Steve
Steve

Reputation: 480

CWD C++ Windows

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

Answers (3)

Macke
Macke

Reputation: 25690

Upvotes: 2

Some programmer dude
Some programmer dude

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:

  • Put the commands (cd and dir and everything else) in to a script file that you run.
  • Change the working directory of your process.

Upvotes: 2

user7860670
user7860670

Reputation: 37578

On Windows you can change working directory using SetCurrentDirectoryW function.

Upvotes: 0

Related Questions