简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM