Oussema Chaabouni
Oussema Chaabouni

Reputation: 689

Why doesn't the system() function work?

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

Upvotes: 1

Views: 768

Answers (1)

unwind
unwind

Reputation: 399793

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.

Upvotes: 6

Related Questions