Save argv to vector or string

I need to save all arguments to a vector or something like this. I am not a programmer, so I don’t know how to do this, but that’s what I still have. I just want to call a function system to pass all arguments after.

#include "stdafx.h" #include "iostream" #include "vector" #include <string> using namespace std; int main ( int argc, char *argv[] ) { for (int i=1; i<argc; i++) { if(strcmp(argv[i], "/all /renew") == 0) { system("\"\"c:\\program files\\internet explorer\\iexplore.exe\" \"www.stackoverflow.com\"\""); } else system("c:\\windows\\system32\\ipconfig.exe"+**All Argv**); } return 0; } 
+4
source share
2 answers

I need to save all arguments to a vector or something

You can use the vector range constructor and pass the appropriate iterators:

 std::vector<std::string> arguments(argv + 1, argv + argc); 

Not sure if 100% is what you asked for. If not, please specify.

+33
source

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

 #include <string> 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\"\""); } } 
0
source

All Articles