Moving from one directory to another?

This is a discussion on Moving from one directory to another? within the C++ Programming forums, part of the General Programming Boards category; Im new to C++ and am trying my best to learn the stuff. I need to figure out the code ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    13

    Moving from one directory to another?

    Im new to C++ and am trying my best to learn the stuff. I need to figure out the code to move from let say c:\My Documents to c:\Program Files with my C++ program. I have no code of my own yet since this is the base of my program so far. What i want it to do is really navigate to say c:\Program Files from where ever the program is saved. Ive tried the system() function but its not helping me i can only get it to display the directory by it being system("dir"). could anyone help me?

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    Why do you need to change the directory? Generally speaking thats not something you commonly have to do.

    When a program needs to access a file in some other directory, it can specify the full path to taht file to get to it.

    There are ways to change the programs working directory, but you won't see any results of that outside of the program.

    To change the working directory you should be able to do this:

    Code:
    #include <direct.h>
    
    int main(int ArgC, char **ArgV)
    {
        _chdir("C:\\Windows");
    
        //Insert other code
    
        return 0;
    }
    Last edited by Exile; 02-02-2005 at 12:05 PM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    I tried that ok here is a sample file I would want to have it open say a program in c:\Program Files\Games\checkers.exe . I tried to do it with what you gave me but no go. Ive asked friends that know C++ but nobody was able to help me. Id like to find out how though. Could be usefull if i want to make a program that would ask if you wanted certain things to be opened.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    Calling a program doesn't require you to be in the directory with the application to start. You should be able to do that with the system() function:

    Code:
    system("c:\\Program Files\\Games\\checkers.exe");
    Thats not the best way, but getting into process control is a bit advanced for a starting program. If you want to get into them take a look at CreateProcess() on MSDN

    Starting a program as a process would look something like this:
    Code:
    PROCESS_INFORMATION ProcInfo;
    STARTUPINFO StartInfo;
    
    StartInfo.cb = sizeof(STARTINFO);
    StartInfo.lpReserved = NULL;
    StartInfo.lpDesktop = NULL;
    StartInfo.lpTitle = NULL;
    StartInfo.cbReserved2 = 0;
    StartInfo.lpReserved2 = NULL;
    StartInfo.dwFlags = 0;
    
    CreateProcess( "c:\\Program Files\\Games\\checkers.exe", NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartInfo, &ProcInfo);

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    i tried this system("c:\\Program Files\\Games\\checkers.exe"); and it didnt do anything to help me. i havent yet tried the other thing you gave me but i just want it to be able to ask a user if they want that program opened or not and if yes to open it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  3. Moving a file to a directory containing spaces
    By notsocks in forum C Programming
    Replies: 11
    Last Post: 06-26-2007, 12:54 AM
  4. Moving files from one directory to multiple dirs
    By csj561 in forum C Programming
    Replies: 7
    Last Post: 03-18-2005, 02:52 PM
  5. Replies: 6
    Last Post: 07-30-2003, 03:08 AM

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