1

As I Know , in cmd , when we want to switch drives we write "[drive]:" exemple : when we want to switch to D:\ we type

D:

and i try this and it work .

But now , I want to apply this process in my C program , so I used the famous "system " command and i type :

system("D:");

and i have some code after that , when i try to execute it , it write

the specified path was not found.

so i tried to see if the system comand really work and i add another system comand like this :

system("chdir");

to verify the working directory and when I execute it , it show me the path of the executable that's mean that the system("D:"); dont work.

any solution please

3
  • What operating system are you using? Commented Jun 16, 2014 at 8:35
  • Why did you decide to use system? Commented Jun 16, 2014 at 8:41
  • I try to modify some files , and I want to go to the directory interactively. If there is an alternative tell me Commented Jun 16, 2014 at 8:44

1 Answer 1

6

Probably because system() starts a new instance of cmd.exe, which runs your command and then exits. Thus, it doesn't hold state between invocations, unlike when you run a single instance and give it multiple commands interactively.

One way of working around this is hinted at by cmd.exe's help text:

Note that multiple commands separated by the command separator '&&' are accepted for string if surrounded by quotes.

So, you should be able to run a command like "d: && chdir" to do both operations in a single invocation of cmd.exe.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.