-3

I wanted to use the system("cd \") function to go to the root directory but it does not work because as I want to create a folder system("md examplecpp"); in this path, a folder is created where I have a program.

  • 1
    system function launches a terminal session that quits after command has been executed. why not just specify the path along with the mkdir/md command – JoshKisb Dec 23 '17 at 17:18
  • 2
    Call system one time. If you want to specify multiple commands separate them with `&&`. With that said the commands you want to execute are better done in `c++` code directly. Also you did not properly escape the \ in your first system() – drescherjm Dec 23 '17 at 17:26
  • Possible duplicate of [Using a Single system() Call to Execute Multiple Commands in C](https://stackoverflow.com/questions/245600/using-a-single-system-call-to-execute-multiple-commands-in-c) – drescherjm Dec 23 '17 at 17:31
  • 1
    The *correct* solution is to not use `system()` at all. Use a system function that specifically creates a folder, like [`CreateDirectory()`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855.aspx) or other equivalent function instead. Any time you have to resort to `system()` there is usually a better way – Remy Lebeau Dec 23 '17 at 22:46

2 Answers2

2

Your problem, is that "the current directory" is a per-process property (although a child process inherits the current directory of its parent as an initial setting). The system function creates a shell process, which executes the cd command (thus changing the current directory of the shell process), and then exits. The current directory of the parent process (your program) is never changed.

Look into the _chdir function (or for Posix chdir)

Edit: You are definitely on Windows - you even put it in the tags!

1

I wanted to use the system("cd \") function to go to the root directory but it does not work because as I want to create a folder system("md examplecpp");

Merge the two (or more) commands together into one command, separated by semicolon, prior to invoking system.

i.e.

 std::string cmd = "cd /home/dmoen ; mkdir examplecpp ; ls -lsa ";
 std::cout << "\nsystem command: " << cmd << "\n" << std::endl;
 std::system (cmd.c_str());

Lesson - the cmd string should look just like you'd type at a command prompt. During testing, you might decide to include a "rmdir examplecpp", as "mkdir" complains if dir already exists.

output (with uninteresting things snipped):

system command: cd /home/dmoen ; mkdir examplecpp ; ls -lsa 

total 402216
     4 drwxr-xr-x 105 dmoen dmoen      4096 Dec 23 11:42 .
     4 drwxr-xr-x   5 root  root       4096 Jan  3  2016 ..
    [snip]
     4 drwxrwxr-x   2 dmoen dmoen      4096 Dec 23 11:42 examplecpp
    [snip]
2785528
  • 5,438
  • 2
  • 18
  • 20
  • Note - my system is Ubuntu 15.10. I forget what shell - probably bash. – 2785528 Dec 23 '17 at 17:49
  • Given that the OP wanted to `cd \ ` for root, they are probably on Windows. Given they tagged the question Windows, they are certainly on Windows (repost with consistent pronouns) – Martin Bonner supports Monica Dec 23 '17 at 20:54
  • I have confirmed that the semicolon separator (between commands) works on the "Command Prompt", as does the "&&" separator identified by drescherjm. – 2785528 Dec 24 '17 at 02:08
  • See also https://stackoverflow.com/a/152142/2785528 - Most answers there suggest that Windows does not have a Unix concept of "\" for root. – 2785528 Dec 24 '17 at 02:13
  • Windows files form a forest, not a tree. Each drive has its own root. Given that most people only have one drive, many Windows programmers (particularly beginners, like the OP), think of it as "the" root. – Martin Bonner supports Monica Dec 24 '17 at 09:27
  • I think my generic answer is this: every OS has more than one shell. I suspect (but have not explored) that most shells will have at least one way to 'chain user commands'. My answer, then, suggests that the OP find out how to chain commands in his system. Two solutions already proposed. Thanks for the forest info about Windows. – 2785528 Dec 24 '17 at 20:19