Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save argv to vector or string

Tags:

c++

argv

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 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;
}
like image 303
user793035 Avatar asked Jun 15 '11 17:06

user793035


People also ask

Can argv be a string?

The argv parameter is an array of pointers to string that contains the parameters entered when the program was invoked at the UNIX command line.

Is argv a string C++?

main(int argc,char *argv[]) // version including the inputs {.... The input argv is an array of strings, one string per item. So argv[0] is the command name, argv[1] is the first input, etc. Numbers on the command line appear in argv as strings, not as numbers.

What does argv hold?

argv is of type char ** . It is not an array. It is a pointer to pointer to char . Command line arguments are stored in the memory and the address of each of the memory location is stored in an array. This array is an array of pointers to char .


1 Answers

i need to save all arguments to a vector or something

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

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

Not 100% sure if that's what you were asking. If not, clarify.

like image 133
fredoverflow Avatar answered Oct 15 '22 05:10

fredoverflow