is it possible to pass string argv[] in main?

Discussion in 'C++' started by Pawel_Iks, Aug 6, 2007.

  1. Pawel_Iks

    Pawel_Iks Guest

    Hello!

    I want to use following statement to pass command-line arguments into
    main function:

    int main(int argc, string argv[])
    {
    if (argc>0)
    {
    //do something
    }

    return 0;
    }

    and it's an error ... do i have to use *char type, and if yes, how to
    convert it into string type which is more diserable for me ... any
    clues, solutions ???
     
    Pawel_Iks, Aug 6, 2007
    #1
    1. Advertising

  2. Pawel_Iks wrote:
    > I want to use following statement to pass command-line arguments into
    > main function:
    >
    > int main(int argc, string argv[])
    > {
    > if (argc>0)
    > {
    > //do something
    > }
    >
    > return 0;
    > }
    >
    > and it's an error ... do i have to use *char type, and if yes, how to
    > convert it into string type which is more diserable for me ... any
    > clues, solutions ???


    Construct a vector of strings:

    if (argc > 0) {
    std::vector<std::string> sargv(argv, argv + argc);
    // do something with 'sargv'
    }

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 6, 2007
    #2
    1. Advertising

  3. Pawel_Iks

    Guest

    On Aug 6, 1:43 pm, Pawel_Iks <> wrote:
    > Hello!
    >
    > I want to use following statement to pass command-line arguments into
    > main function:
    >
    > int main(int argc, string argv[])
    > {
    > if (argc>0)
    > {
    > //do something
    > }
    >
    > return 0;
    >
    > }
    >
    > and it's an error ... do i have to use *char type, and if yes, how to
    > convert it into string type which is more diserable for me ... any
    > clues, solutions ???


    Yes, you have to use char**.
    You can convert it like so:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    #include <string>

    int main( int argc, char* argv[] )
    {
    std::vector< std::string > args;

    for( int i = 0; i != argc; ++i )
    args.push_back( std::string( argv ));

    std::copy( args.begin(), args.end()
    , std::eek:stream_iterator<std::string>( std::cout, "\n" ));
    }
     
    , Aug 6, 2007
    #3
  4. Pawel_Iks

    James Kanze Guest

    On Aug 6, 10:50 pm, "Victor Bazarov" <> wrote:
    > Pawel_Iks wrote:
    > > I want to use following statement to pass command-line arguments into
    > > main function:


    > > int main(int argc, string argv[])
    > > {
    > > if (argc>0)
    > > {
    > > //do something
    > > }


    > > return 0;
    > > }


    > > and it's an error ... do i have to use *char type, and if yes, how to
    > > convert it into string type which is more diserable for me ... any
    > > clues, solutions ???


    > Construct a vector of strings:


    > if (argc > 0) {
    > std::vector<std::string> sargv(argv, argv + argc);
    > // do something with 'sargv'
    > }


    Just for the record, you don't need the if; argc is guaranteed
    to be >= 1. (If the name of the program isn't available---never
    the case under Windows or Unix---, then argv[0] is the empty
    string.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 7, 2007
    #4
  5. Pawel_Iks

    Guest

    On Aug 7, 12:40 pm, James Kanze <> wrote:
    > On Aug 6, 10:50 pm, "Victor Bazarov" <> wrote:
    >
    >
    >
    >
    >
    > > Pawel_Iks wrote:
    > > > I want to use following statement to pass command-line arguments into
    > > > main function:
    > > > int main(int argc, string argv[])
    > > > {
    > > > if (argc>0)
    > > > {
    > > > //do something
    > > > }
    > > > return 0;
    > > > }
    > > > and it's an error ... do i have to use *char type, and if yes, how to
    > > > convert it into string type which is more diserable for me ... any
    > > > clues, solutions ???

    > > Construct a vector of strings:
    > > if (argc > 0) {
    > > std::vector<std::string> sargv(argv, argv + argc);
    > > // do something with 'sargv'
    > > }

    >
    > Just for the record, you don't need the if; argc is guaranteed
    > to be >= 1. (If the name of the program isn't available---never
    > the case under Windows or Unix---, then argv[0] is the empty
    > string.)
    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide quoted text -
    >
    > - Show quoted text -


    are you learning in niit?
     
    , Aug 7, 2007
    #5
  6. wrote:
    > int main( int argc, char* argv[] )
    > {
    > std::vector< std::string > args;
    >
    > for( int i = 0; i != argc; ++i )
    > args.push_back( std::string( argv ));


    Why do it like that when you can do it more easily like:


    int main( int argc, char* argv[] )
    {
    std::vector< std::string > args(argv, argv+argc);
     
    Juha Nieminen, Aug 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,628
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    6,006
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,681
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    937
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    679
    Chris Torek
    Dec 8, 2004
Loading...

Share This Page