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:

  1. change command given system:

    system("../folder1/folder2/executable_name"); 
  2. change command given system:

    this going work if default shell bash, , many of unix shells.

    system("cd ../folder1/folder2; ./executable_name"); 
  3. create shell script has:

    #/bin/bash cd  ../folder1/folder2 ./executable_name 

    and run shell script c using system

    system("myscript.sh"); 
  4. add line chdir in c before call system:

    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

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -