You can use chdir to force this change of directory 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");
}
InwindowswecanhavethesameresultwithSetCurrentDirectory
#include<stdio.h>#include<stdlib.h>#include<windows.h>intmain(void){//apontamosoterminalparac:\SetCurrentDiretory("c:\");
system("dir");
//apontamos o terminal para c:\windows
SetCurrentDiretory("c:\windows");
system("dir");
}
If you put the commands in the same system separated by a semicolon, it also works (in windows you use & to separate), you can always use the cd when calling the system pass it to 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 mentioned by Kyale A
#include <stdio.h>
#include <unistd.h>
int main(void){
execv("/bin/sh",NULL);
}
There are other forms besides those mentioned ^^