Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

c++ - changing the directory from inside a c program under windows using system command

I have a problem where I have to run a command prompt command from inside a C program. Here is what I did

#include<stdio.h>
#include<stdlib.h>


int main(){



system("cd Users");
system("Dir");

}

The problem is I want to cd into Users first and then execute the command Dir where as currently the program outputs that it cant find the path specified and the Dir is executed in the parent process directory i-e where the program source file is located.

The actual task I want to do is just want to run a java file from a particular directory from inside a C program. that java file is in C:UsersAbdullahpro . My C program's parent directory is C:UsersCprog. Please advise on how may I do this

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

system() starts a new process. This new process changes its current directory, then ends. The current directory of your program's process does not change.

You want chdir() (or _chdir()).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...