473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8631
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1624
by: efiryago | last post by:
Is there a way to calculate size of a table without runnig statistics to use NPAGES info. In my case there are a few very large tables in the same tablespace with very outdated stats, so LIST...
5
16315
by: No Spam | last post by:
This is probably an easy question, but I have not used C for quite some time: How do I determine the size of an array? More specifically, how do I determine the number of incoming parameters? ...
22
411
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
2
4100
by: Jack | last post by:
Anybody know how to determine the "sizeof" a class? TYIA Jack jackmcgillis@netscape.NOSPAM.net
2
1486
by: mykidisgollum | last post by:
Greetings, We've recently been working with our site pages' viewstate, turning off unnecessesary viestate items to reduce page size for faster loading. We are also experimenting with using less...
7
1841
by: serrand | last post by:
Hello all, i wonder if we can know the size of a variable from a pointer : static void *my_free_hook (void *ptr, const void *caller) { size_t sz; /* bla bla bla */ /* HOW CAN I DETERMINE...
5
446
by: bob | last post by:
is there any way to get the size of a function in c++? for instance, you might want to move it in memory or something.
12
1738
by: garyusenet | last post by:
I want to write a program that lists all folders on my local hard drive in order of size, any ideas for how I might do this? Thankyou, Gary.
1
9170
by: breal | last post by:
Forgive me for this question which is most likely stupid... How do I determine the number of bytes a string takes up? I have a soap server that is returning a serialized string. It seems that...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.