473,323 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Command line argument problems

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
Jul 22 '05 #1
2 1686

"Justin Naidl" <jn***@lsol.net> wrote in message
news:Pe*******************@monger.newsread.com...
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
Jul 22 '05 #2
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eli Criffield | last post by:
I'm try run an ssh command in pexpect and I'm having trouble getting everything escaped to do what i want. Here's a striped down script showing what i want to do. -- #!/usr/bin/env python...
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
7
by: John Wildes | last post by:
Hello All, I've got a VB.net windows form app that I have created to solve some recurring maintenance tasks. What I would like to do is build functionality into this application so that accepts...
5
by: repairman2003 | last post by:
I'm writing a command prompt for unix and I've run into some problems: #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include...
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.