469,953 Members | 1,998 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

Determine size of argv[x]

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).

Mar 13 '07 #1
3 7467
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]).

Mar 13 '07 #2
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
Mar 13 '07 #3
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::stringparams(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()
Mar 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by No Spam | last post: by
2 posts views Thread by Jack | last post: by
2 posts views Thread by mykidisgollum | last post: by
7 posts views Thread by serrand | last post: by
5 posts views Thread by bob | last post: by
1 post views Thread by breal | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.