How do I run an executable in a different directory in C? -
i want run executable found in ../folder1/folder2 while inside of c script. right i'm trying do:
char command[50]; strcpy(command, "cd ../folder1/folder2"); system(command); memset(command,0,sizeof(command)); strcpy(command, "./executable_name"); system(command); but it's not working. should using chdir(), or there way this? possible?
it's not working because when execute:
system("cd ../folder1/folder2"); it not have effect on current directory of executable. hence, when execute:
system("./executable_name"); it doesn't find it.
you can solve using of following methods:
change command given
system:system("../folder1/folder2/executable_name");change command given
system:this going work if default shell
bash, , many of unix shells.system("cd ../folder1/folder2; ./executable_name");create shell script has:
#/bin/bash cd ../folder1/folder2 ./executable_nameand run shell script c using
systemsystem("myscript.sh");add line
chdirin c before callsystem:chdir("../folder1/folder2"); system("./executable_name");
update, due @jongware
all of above assume program executed in directory ../folder1/folder2 valid path. if program executed different directory , want account scenario, have parse argv[0] , adjust way handle calls execute executable_name.
Comments
Post a Comment