I want to use the system() function to change directories, and run a few programs based on user input.
Code so far:
I also tried omitting the whole cd_cmd array and doing this:Code:#include <stdio.h> int main() { int MAX_PATH = 100; int i; char path[MAX_PATH]; char cd_cmd[MAX_PATH + 3]; printf("Please enter sox src directory: "); fgets(path,MAX_PATH,stdin); for(i = 0; i < MAX_PATH; i++) { if(path[i] == '\n') { path[i] = '\0'; break; } } cd_cmd[0] = 'c'; cd_cmd[1] = 'd'; cd_cmd[2] = ' '; for(i = 0; i < MAX_PATH; i++) { cd_cmd[i+3] = path[i]; } printf("cd_cmd is now %s \n", cd_cmd); system("ls -l"); system(cd_cmd); system("ls -l"); return(0); }
that compiled, but didn't change the directory like I wanted it to.Code:system("cd ", path);
So how do I get a path from a user and change the current directory to that?
Thanks!