Determine size of argv[x]

Discussion in 'C++' started by John Williams, Mar 13, 2007.

  1. I need a way to determine how many characters are in a command line
    argument. Basically the code snippet looks like
    int main(int argc, char *argv[])
    {
    openfiles(argv[1], argv[2], &infile, &outfile);
    cipher(&infile, &outfile, argv[3]);
    return 0;
    }

    void cipher(fstream *infile, fstream *outfile, char key[])
    {
    plain text in
    bring in the key
    cipher text out
    }

    Right now the entire program works, because I have it set up to only
    accept 8 character keys. I'd like to set it up so that it can determine
    at runtime how many characters the key is. Since the key is accepted at
    the command line I can't figure out how it's done since sizeof(argv[3])
    will always return the sizeof a pointer to a char (4) and
    sizeof(*argv[3]) seems to always return the size of a single char (1).
     
    John Williams, Mar 13, 2007
    #1
    1. Advertising

  2. John Williams

    Rolf Magnus Guest

    John Williams wrote:

    > I need a way to determine how many characters are in a command line
    > argument. Basically the code snippet looks like
    > int main(int argc, char *argv[])
    > {
    > openfiles(argv[1], argv[2], &infile, &outfile);
    > cipher(&infile, &outfile, argv[3]);
    > return 0;
    > }
    >
    > void cipher(fstream *infile, fstream *outfile, char key[])
    > {
    > plain text in
    > bring in the key
    > cipher text out
    > }
    >
    > Right now the entire program works, because I have it set up to only
    > accept 8 character keys. I'd like to set it up so that it can determine
    > at runtime how many characters the key is. Since the key is accepted at
    > the command line I can't figure out how it's done since sizeof(argv[3])
    > will always return the sizeof a pointer to a char (4) and
    > sizeof(*argv[3]) seems to always return the size of a single char (1).


    try strlen(argv[3]).
     
    Rolf Magnus, Mar 13, 2007
    #2
    1. Advertising

  3. Rolf Magnus wrote:
    > John Williams wrote:
    >
    >> I need a way to determine how many characters are in a command line
    >> argument. Basically the code snippet looks like
    >> int main(int argc, char *argv[])
    >> {
    >> openfiles(argv[1], argv[2], &infile, &outfile);
    >> cipher(&infile, &outfile, argv[3]);
    >> return 0;
    >> }
    >>
    >> void cipher(fstream *infile, fstream *outfile, char key[])
    >> {
    >> plain text in
    >> bring in the key
    >> cipher text out
    >> }
    >>
    >> Right now the entire program works, because I have it set up to only
    >> accept 8 character keys. I'd like to set it up so that it can determine
    >> at runtime how many characters the key is. Since the key is accepted at
    >> the command line I can't figure out how it's done since sizeof(argv[3])
    >> will always return the sizeof a pointer to a char (4) and
    >> sizeof(*argv[3]) seems to always return the size of a single char (1).

    >
    > try strlen(argv[3]).
    >


    Perfect thanks
     
    John Williams, Mar 13, 2007
    #3
  4. Rolf Magnus wrote:
    > try strlen(argv[3]).


    Another possibility, which is especially worth considering if you
    are not comfortable with handling C-style strings (which are just
    char pointers) is to create a vector of std::strings from the
    command-line parameters. These will be safer and much easier to
    handle than the "raw" argv array.

    It's very easy to create a vector of strings in C++ (surprisingly
    easy for those who have never thought of it before):

    int main(int argc, char* argv[])
    {
    std::vector<std::string> params(argv, argv+argc);
    ...
    }

    Now you can index 'params' for the command-line parameters
    ('params.size()' will tell you how many there are), and each element
    will be an std::string, which is easier and safer to handle than the
    char*'s. So, for example, if you want to know the size of the fourth
    parameter: params[3].size()
     
    Juha Nieminen, Mar 13, 2007
    #4
    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,597
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    5,962
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,646
    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:
    909
    =?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:
    668
    Chris Torek
    Dec 8, 2004
Loading...

Share This Page