Save argv to vector or string

后端 未结 2 817
面向向阳花
面向向阳花 2020-12-16 19:36

I need to save all arguments to a vector or something like this. I\'m not a programmer, so I don\'t know how to do it, but here\'s what I\'ve got so far. I just want to call

2条回答
  •  不知归路
    2020-12-16 20:30

    To build string with all argument concatenated and then run a command based on those arguments, you can use something like:

    #include 
    using namespace std;
    string concatenate ( int argc, char* argv[] )
    {
        if (argc < 1) {
            return "";
        }
        string result(argv[0]);
        for (int i=1; i < argc; ++i) {
            result += " ";
            result += argv[i];
        }
        return result;
    }
    int main ( int argc, char* argv[] )
    {
        const string arguments = concatenate(argc-1, argv+1);
        if (arguments == "/all /renew") {
            const string program = "c:\\windows\\system32\\ipconfig.exe";
            const string command = program + " " + arguments;
            system(command.c_str());
        } else {
            system("\"\"c:\\program files\\internet explorer\\iexplore.exe\" \"www.stackoverflow.com\"\"");
        }
    }
    

提交回复
热议问题