chdir()

This is a discussion on chdir() within the C++ Programming forums, part of the General Programming Boards category; i'm currently using chdir() in my program, and I got it to work...however... I don't know how to cd to ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    36

    chdir()

    i'm currently using chdir() in my program, and I got it to work...however... I don't know how to cd to the root directory, or how to cd up one directory for that matter....here's the code...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <unistd.h>
    
    void main()
    {
            char cmd[100] = " ";
            char dir[100] = " ";
            char * pch;
            int cmdLength, cmdCounter;
            int dirCounter = 0;
    
            cout << "Please enter command: ";
            cin.getline(cmd, 100);
    
            while (strcmp (cmd, "exit") !=0)
            {
                    if((cmd[0] == 'c') && (cmd[1] == 'd') && (cmd[2] == ' '))
                    {
                            cout << "I'm here corey..." << endl;
                            cmdLength = strlen(cmd);
                            cout << cmdLength << endl;
                            for(cmdCounter=3; cmdCounter < cmdLength; cmdCounter++)
                            {
                                    cout << dir[dirCounter];
                                    dir[dirCounter] = cmd[cmdCounter];
                                    dirCounter++;
                            }
                            chdir(dir);
                            cout << "Please enter command: ";
                            cin.getline(cmd,100);
                    }
                    else
                    {
                            system (cmd);
                            cout << "Please enter command: ";
                            cin.getline(cmd,100);
                    }
            }
            exit(1);
    }

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    If you're using windows this is a good start

    http://www.adrianxw.dk/SoftwareSite/...irstFile1.html

    Type this in the command line to change directory:
    Code:
    cd c:\program files

    Using system:

    Code:
    system("cd c:\\program files");
    perhaps?
    Last edited by treenef; 11-18-2005 at 03:12 AM.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Code:
    C:\Documents and Settings\Owner>  cd \
    C:\> cd \Documents And Settings\Owner\Desktop
    C:\Documents And Settings\Owner\Desktop> cd ..\
    C:\Documents And Settings\Owner> cd .\Desktop
    C:\Documents And Settings\Owner\Desktop> cd ..\..\
    C:\Documents And Settings>
    
    ..\    Down Directory
    .\     Up Directory
    \      Root Directory
    void main() // int main()
    Last edited by SlyMaelstrom; 11-18-2005 at 04:02 PM.
    Sent from my iPadŽ

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    C:\> cd \Documents And Settings\Owner\Desktop
    C:\Documents And Settings\Owner\Desktop> ..\
    
    .\     Up Directory
    ..\    Down Directory
    ->
    Code:
    C:\> cd "\Documents And Settings\Owner\Desktop"
    C:\Documents And Settings\Owner\Desktop>cd ..\
    
    .\     Current Directory
    ..\    Up Directory
    Code:
    system("cd c:\\program files");
    ->
    Code:
    system("cd \"C:\\program files\"");
    The closing quotes are optional.

    Some systems support ...\ as a synonym for ..\..\. On UNIX you use forward slashes (/).
    Last edited by dwks; 11-18-2005 at 03:58 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with chdir()
    By ammochck21 in forum C Programming
    Replies: 8
    Last Post: 09-11-2008, 10:26 PM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Replies: 12
    Last Post: 04-25-2007, 03:48 PM
  4. how to do...
    By kirmelyte in forum C Programming
    Replies: 1
    Last Post: 05-17-2006, 01:59 AM
  5. Chdir()
    By Calavera in forum C Programming
    Replies: 1
    Last Post: 11-30-2004, 01:13 PM

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