Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Command line argument problems

Reply
Thread Tools

Command line argument problems

 
 
Justin Naidl
Guest
Posts: n/a
 
      09-17-2004
I'm having trouble getting some arguments to work from the command line. A
program I have excepts three arguments: "pgm argA argB argC. The first two
arguments are filenames the last is an integer. I am having no trouble with
the first two however the third is giving me some trouble. If I enter the
value '3' as the third argument and cout it it displays '3' on the screen.
However, I need to use it as an integer and if I try to cast it as an
integer I get the number '-1073743214'. Does anybody no how to fix this or
what the problem might be? I've tried a number of different castings. I
understand that the arguments value is a 'char *' my main looks like this.
int main(int argc, char* argv[])
{
stuff
}

I pass it to a function
cksum(argv[2],(int)(argv[3]));

I get no errors just the tainted value. Any suggestions?

Thanks
~Justin


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-17-2004

"Justin Naidl" <(E-Mail Removed)> wrote in message
newseJ2d.1673$(E-Mail Removed)...
> I'm having trouble getting some arguments to work from the command line.
> A
> program I have excepts three arguments: "pgm argA argB argC. The first
> two
> arguments are filenames the last is an integer. I am having no trouble
> with
> the first two however the third is giving me some trouble. If I enter the
> value '3' as the third argument and cout it it displays '3' on the screen.
> However, I need to use it as an integer and if I try to cast it as an
> integer I get the number '-1073743214'. Does anybody no how to fix this
> or
> what the problem might be? I've tried a number of different castings. I
> understand that the arguments value is a 'char *' my main looks like
> this.
> int main(int argc, char* argv[])
> {
> stuff
> }
>
> I pass it to a function
> cksum(argv[2],(int)(argv[3]));
>
> I get no errors just the tainted value. Any suggestions?
>
> Thanks
> ~Justin
>


You misunderstand what a cast is. What you need is a conversion not a cast.
Simplest conversion function to use is atoi, declared in <stdlib.h>

cksum(argv[2],atoi(argv[3]));

atoi does no error checking however. strtol is better is you want to check
for errors.

john


 
Reply With Quote
 
 
 
 
Niels Dekker - no reply address
Guest
Posts: n/a
 
      09-18-2004
Justin Naidl wrote:
[...]
> If I enter the value '3' as the third argument and cout it it
> displays '3' on the screen. However, I need to use it as an integer
> and if I try to cast it as an integer I get the number '-1073743214'.
> Does anybody no how to fix this or what the problem might be? I've
> tried a number of different castings.

[...]
>
> I pass it to a function
> cksum(argv[2],(int)(argv[3]));
>
> I get no errors just the tainted value. Any suggestions?


John Harrison replied:
>
> You misunderstand what a cast is. What you need is a conversion not a cast.
> Simplest conversion function to use is atoi, declared in <stdlib.h>
>
> cksum(argv[2],atoi(argv[3]));
>
> atoi does no error checking however. strtol is better is you want to check
> for errors.


I'm not so sure if the OP misunderstands the word "cast"... Boost's
lexical_cast should do the job as well:

#include <boost/lexical_cast.hpp> // From www.boost.org

try
{
cksum(argv[2], boost::lexical_cast<int>(argv[3]));
}
catch(const boost::bad_lexical_cast &)
{
std::cout
<< "Lexical cast from \"" << argv[3] << "\" to int failed"
<< std::endl;
}

Regards,

Niels Dekker
www.xs4all.nl/~nd/dekkerware
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java -jar ignore the classpath command line argument Thomas Kellerer Java 5 04-13-2005 12:01 PM
What is the best way to handle a command line argument that includes an escape sequence like \n? Joe Python 8 03-04-2005 02:45 AM
Program cannot recognize asterisk(*) as an argument passed from the command line? Fong Java 3 05-27-2004 01:55 AM
ANNOUNCE: SCons 0.94 adds command-line argument features, fixeskey bugs Steven Knight Python 0 11-07-2003 04:56 PM
reading in command line argument nic977 C Programming 13 09-22-2003 02:50 AM



Advertisments