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

  1. #1
    Registered User
    Join Date
    May 2005

    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:

    #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);
    	strcat(cd, name);
    	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
    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
    The edge of the known universe
    > 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
    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.

  4. #4
    Registered User
    Join Date
    May 2005
    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 10: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, 08: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, 05: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, 03:58 PM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM