0

I am working on a unix-like file system and I need to take in commands from the command line. Some commands are one argument like mkfs, ls, etc. But some are two commands like mkdir foo, rmfile hello. My question is, how can I parse the string to take in a command (cmd), but also take in a second argument (next)? Here is my code so far: (I have not included the functions for the sake of space)

int main(int argc, char *argv[]){

    string cmd;
    string next;


    while (1){
        cout << "Russ_John_Shell> ";
        cin >> cmd;



        //Checks to see whether the string has a file/directory name after the command
        if (next == ""){
            //mkfs command
            if (cmd == "mkfs"){
                makeFS();
            }
            //exit command
            else if (cmd == "exit"){
                exitShell();
            } 
            //ls command
            else if (cmd == "ls"){
                listDir();
            }
            //command not recognized at all
            else {
                printf("Command not recognized.\n");
            }

        }
        else{

            //mkdir command
            if (cmd == "mkdir"){
                makeDir(next);
            }
            //rmdir command
            else if (cmd == "rmdie"){
                remDir(cmd);
            }
            //cd command
            else if (cmd == "cd"){
                changeDir(next);
            }
            //stat command
            else if (cmd == "stat"){
                status(next);
            }
            //mkfile command
            else if (cmd == "mkfile"){
                makeFile(next);
            }
            //rmfile command
            else if (cmd == "rmfile"){
                remFile(next);
            }
            //command not recognized at all
            else {
                printf("Command not recognized.\n");
            }
        }
    }
}

Thanks in advance for any help!

MostPalone
  • 77
  • 7
  • Use [`std::string::find()`](http://en.cppreference.com/w/cpp/string/basic_string/find) to look for whitespace, and if found then use [`std::string::substr()`](http://en.cppreference.com/w/cpp/string/basic_string/substr) to extract substrings on either side of the whitespace. Or, look at using [`std::istringstream`](http://en.cppreference.com/w/cpp/io/basic_istringstream) with [`operator>>`](http://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt) or [`std::getline()`](http://en.cppreference.com/w/cpp/string/basic_string/getline) to read whitespace-separated substrings. – Remy Lebeau Nov 30 '16 at 04:24
  • argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied, and *argv[n] is the last argument.argv[1] will contains the command and argv[2] will contains the first argument. – user1438832 Nov 30 '16 at 04:26
  • @user1438832: that only applies when processing command-line arguments that are passed to the program when it first starts. That does not apply when parsing other kinds of strings, like user-prompted values once the program is up and running, like shown in the OP's example. – Remy Lebeau Nov 30 '16 at 04:28
  • 1
    Sorry i didnt see that.use istringstream iss(cmd); vector tokens; copy(istream_iterator(iss), istream_iterator(), back_inserter(tokens)); – user1438832 Nov 30 '16 at 04:31

0 Answers0