Voce can use chdir to force this directory change to the system on linux
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void){
//apontamos o terminal para /
chdir("/");
system("pwd");
//apontamos o terminal para /home/kodonokami
chdir("/home/kodonokami");
system("pwd");
}
in windows we may have the same result with Setcurrentdirectory
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(void){
//apontamos o terminal para c:\
SetCurrentDiretory("c:\\");
system("dir");
//apontamos o terminal para c:\windows
SetCurrentDiretory("c:\\windows");
system("dir");
}
Placing commands on the same system separated by a period and comma also works (in windows & to separate is used), Voce can always use the cd when calling the system pass to it a variable that has the current directory and modify this variable when you need to change the directory
#include <stdio.h>
#include <stdlib.h>
int main(void){
//isso é uma gambiarra '-'
system("cd / ; pwd ; cd /home/kodonokami ; pwd");
}
another way can be done by running the shell by the program as already quoted by Kyale A
#include <stdio.h>
#include <unistd.h>
int main(void){
execv("/bin/sh",NULL);
}
has other forms besides those cited ^^
You also have an example of how to use 'Fork' and 'exec' in English here: https://answall.com/questions/142384/h%C3%A1-some-way-to-run-a-java-jar-a-from-a-program-c/142509#142509
– Kyle A
Lacobus' answers are great =]
– Jefferson Quesado