Can't change directories via system("cd");

This is a discussion on Can't change directories via system("cd"); within the C++ Programming forums, part of the General Programming Boards category; Hey everybody, I am trying to make a simple program that creates a directory, switches to it, creates another, switches ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    Can't change directories via system("cd");

    Hey everybody,
    I am trying to make a simple program that creates a directory, switches to it, creates another, switches to that, and then proceeds to make a file. I first thought of using a batch file, but input is messy (if not impossible). I have been trying to use system("cd directory");, but it doesn't change directories. Here is the code:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    int main()
    {
    	char name[20];
    	char md[24] = "MD ";
    	char cd[24] = "CD ";
    	cin >> name;
    	strcat(md, name);
    	system(md);
    	strcat(cd, name);
    	system(cd);
    	system("MD Classes");
    	system("CD Classes");
    	strcat(name, ".uc");
    	ofstream createFile (name);
    	return 0;
    }
    When run, it creates the two directories and the .uc file in the program's directory (I want the .uc file inside of one directory, which is inside of the other directory). Can programs not change what directory the are operating in? Any help with a fix would be appreciated. Thanks.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    kind of a pointless program isn't it? Why not just make a .bat script?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > system(md);
    Whilst this changes directory, it does so only for the duration of the process it creates. As soon as it finishes, you're back in the directory your program is working in.

    You need to use the function calls
    chdir()
    mkdir()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    2
    Thanks alot for the reply. I wasn't aware that chdir() and mkdir() existed. Live and learn.

    Oh, and as for the batch issue, I wasn't aware that you could get string input from a batch. Is there something other than choice and reply? Plus, the purpose of this program was more to increase my knowledge of file system manipulation than for practical purpose.
    Last edited by Frosty; 05-07-2005 at 11:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++builder6 change form names problem
    By Leite33 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2008, 09:20 AM
  2. how to change static char* and not lose mem ?
    By jabka in forum C Programming
    Replies: 15
    Last Post: 09-07-2007, 06:33 PM
  3. Replies: 2
    Last Post: 11-08-2002, 03:22 AM
  4. help with calculating change from a dollar
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2002, 04:58 PM
  5. Replies: 2
    Last Post: 09-04-2001, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21