Thread: a little help with system()

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

    a little help with system()

    I am writing a program that acts like a shell. I used the System() function to compute the commands, however, I realized that exit and cd do not work. I figured out how to make my own exit, however, I cannot figure out how to get cd to work...if anyone could help that'd be great, I appreciate it in advance....here's my code...I wasn't sure which forum would be good to post this in so i posted it in the linux one also

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    
    void main()
    {
            char cmd[100] = " ";
    
            cout << "Please enter command: ";
            cin.getline(cmd, 100);
    
            while (strcmp (cmd, "exit") !=0)
                    {
    
                            system (cmd);
                            cout << "Please enter command: ";
                            cin.getline(cmd,100);
                    }
                    exit(1);
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Every time you use system, it creates a new console, so any changes made using a command in one call to system won't carry over to later calls to system. That means you cannot use system to do the equivalent of cd.

    BTW, strcmp is in <cstring>, not <string> and you should specify int as the return type for main, not void.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you need to use _chdir() function. You should use c functions as much as possible because they are faster and take less memory.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    thanks for the input...but how would i go about doing the cd function?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    what is the name of the include file that _chdir() function is in??

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    http://www.google.com/search?hl=en&q=man+chdir

    depending on your compiler it may or may not start with the underscore.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    ok..now my only problem is figuring out a way to get the program to distinguish between the cd part, and the directory that it wants to go to...

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    this is the updated code...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <unistd.h>
    
    void main()
    {
            char cmd[100] = " ";
            char * pch;
    
            cout << "Please enter command: ";
            cin.getline(cmd, 100);
    
            while (strcmp (cmd, "exit") !=0)
            {
                    if((cmd[0] == "c") && (cmd[1] == "d"))
                    {
                            pch = strtok(cmd," ");
                            pch = strtok(NULL," ");
                            chdir(pch);
                    }
                    else
                    {
                            system (cmd);
                            cout << "Please enter command: ";
                            cin.getline(cmd,100);
                    }
            }
            exit(1);
    }
    and there are the errors im recieving from my linux compiler

    test.cpp:16: ISO C++ forbids comparison between pointer and integer
    test.cpp:16: ISO C++ forbids comparison between pointer and integer

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    any ideas?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    cmd[0] is the first character of the string. "c" is a string literal. You want that to be a character literal, which uses single quotes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 01:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM
  5. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM