I am trying to read in couple things from the command line argument, and
one of them suppose to be a integer, but when i try to cast it from a
char * to an integer, it gives strange number...... any idea how should
i do it....
this is the way i do it
void main (int argc, char *argv[])
{
....
int n;
n = (int) argv[1];
....
}
--
Posted via http://dbforums.com 13 8308
nic977 <me*********@dbforums.com> scribbled the following: I am trying to read in couple things from the command line argument, and one of them suppose to be a integer, but when i try to cast it from a char * to an integer, it gives strange number...... any idea how should i do it....
You're not supposed to cast it to an integer. Look up strtol().
this is the way i do it
void main (int argc, char *argv[])
Undefined behaviour. This should be:
int main(int argc, char *argv[])
{
....
int n;
n = (int) argv[1];
As I said, this is the wrong way altogether. strtol() is your friend.
....
}
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am looking for myself. Have you seen me somewhere?"
- Anon
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bj**********@oravannahka.helsinki.fi... nic977 <me*********@dbforums.com> scribbled the following: I am trying to read in couple things from the command line argument, and one of them suppose to be a integer, but when i try to cast it from a char * to an integer, it gives strange number...... any idea how should i do it....
You're not supposed to cast it to an integer. Look up strtol().
I always use atol. Is there any difference between them ?
--
Jeff
-je6543 at yahoo.com
In article <bj*********@imsp212.netvigator.com>, Jeff wrote: "Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message news:bj**********@oravannahka.helsinki.fi... nic977 <me*********@dbforums.com> scribbled the following: > I am trying to read in couple things from the command line argument, and > one of them suppose to be a integer, but when i try to cast it from a > char * to an integer, it gives strange number...... any idea how should > i do it....
You're not supposed to cast it to an integer. Look up strtol().
I always use atol. Is there any difference between them ?
atol(const char *nptr)
is equivalent to
strtol(nptr, (char **)NULL, 10)
.... but atol doesn't affect errno in any way. Also for atol, if
the value in the result can not be represented, the behaviour is
undefined.
--
Andreas Kähäri
nic977 wrote: I am trying to read in couple things from the command line argument, and one of them suppose to be a integer, but when i try to cast it from a char * to an integer, it gives strange number...... any idea how should i do it....
void main (int argc, char *argv[])
There are so many books out there that will tell you to use void
main. Please ignore them on this point. Your program will return a
value to its calling environment whether you tell it to or not. The
environment may expect this value to be meaningful and use it for
error checking or other purposes. If you use void main, it won't be
meaningful, and things will break.
n = (int) argv[1];
argv[1] (assuming there is one, which you should check) is a pointer
to char - an ordered collection of characters, such as "1234". If
you cast this pointer to int, you'll get some undefined value. You
need to use a conversion function such as atoi(), atol(), or
strtol().
--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
In article <bj*********@imsp212.netvigator.com>, Jeff wrote: "Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message news:bj**********@oravannahka.helsinki.fi... nic977 <me*********@dbforums.com> scribbled the following: > I am trying to read in couple things from the command line > argument, and one of them suppose to be a integer, but when > i try to cast it from a char * to an integer, it gives > strange number...... any idea how should i do it....
You're not supposed to cast it to an integer. Look up strtol().
I always use atol. Is there any difference between them ?
Yes. Error and overflow detection are much easier with strtol.
--
Neil Cerutti
Andreas Kahari wrote: In article <bj*********@imsp212.netvigator.com>, Jeff wrote: I always use atol. Is there any difference between them ?
[snip]
Also, according to this manpage I'm reading, strtol isn't ANSI. So
it may not be available on some systems.
--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
In article <3F***************@pobox.com>, Tom Zych wrote:
[cut] Also, according to this manpage I'm reading, strtol isn't ANSI. So it may not be available on some systems.
I'm sorry to hear that. Please inform the maintainer that it
needs updating. In the standard I'm holding in my hand, it says
it's very much a standard library function defined in <stdlib.h>
(C99, 7.20.1.4).
--
Andreas Kähäri
In article <sl**********************@vinland.freeshell.org> ,
Andreas Kahari wrote: In article <3F***************@pobox.com>, Tom Zych wrote: [cut] Also, according to this manpage I'm reading, strtol isn't ANSI. So it may not be available on some systems.
[cut my reply]
In fact, it was added for C89.
--
Andreas Kähäri
Andreas Kahari wrote: In fact, it was added for C89.
Hmm, you're right. It's right here in K&R2. And (FWIW) gcc -ansi
-pedantic has no problem with it.
manpage says:
CONFORMING TO
strtol() conforms to SVID 3, BSD 4.3, ISO 9899 (C99)
and POSIX, and strtoll() to ISO 9899 (C99) and POSIX
1003.1-2001.
I just did a little research and found that ISO 9899 = ANSI C. But,
this says ISO9899 (C99), which sounds like they're saying it wasn't
in C89. Misleading, at least. Or maybe not to more experienced
people?
--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Originally posted by Neil Cerutti In article <bj*********@imsp212.netvigator.com>, Jeff wrote:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bj**********@oravannahka.helsinki.fi"]news:bjsktd$rs- u$*@oravannahka.helsinki.fi[/url]...
nic977 <me*********@dbforums.com> scribbled the following:
> I am trying to read in couple things from the command line > argument, and one of them suppose to be a integer, but when > i try to cast it from a char * to an integer, it gives
> strange number...... any idea how should i do it....
You're not supposed to cast it to an integer. Look up strtol().
I always use atol. Is there any difference between them ?
Yes. Error and overflow detection are much easier with strtol.
--
Neil Cerutti
Thanks, they really help
--
Posted via http://dbforums.com
Tom Zych wrote: Also, according to this manpage I'm reading, strtol isn't ANSI. So it may not be available on some systems.
Your manpage is in error.
--
Martin Ambuhl
Originally posted by Neil Cerutti In article <bj*********@imsp212.netvigator.com>, Jeff wrote:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bj**********@oravannahka.helsinki.fi"]news:bjsktd$rs- u$*@oravannahka.helsinki.fi[/url]...
nic977 <me*********@dbforums.com> scribbled the following:
> I am trying to read in couple things from the command line > argument, and one of them suppose to be a integer, but when > i try to cast it from a char * to an integer, it gives
> strange number...... any idea how should i do it....
You're not supposed to cast it to an integer. Look up strtol().
I always use atol. Is there any difference between them ?
Yes. Error and overflow detection are much easier with strtol.
--
Neil Cerutti
Thanks, they really help
--
Posted via http://dbforums.com
On Fri, 12 Sep 2003 14:56:18 +0000 (UTC), Andreas Kahari
<ak*******@freeshell.org> wrote: In article <bj*********@imsp212.netvigator.com>, Jeff wrote:
<snip> I always use atol. Is there any difference [from strtol]?
atol(const char *nptr)
is equivalent to
strtol(nptr, (char **)NULL, 10)
(Or NULL without the cast, if a prototype declaration is in scope.)
... but atol doesn't affect errno in any way. Also for atol, if the value in the result can not be represented, the behaviour is undefined.
Nit: atol is not *required* to set errno, where strtol is for errors;
it *may*, like all library functions that don't have more specific
requirements, not to mention all UB; and *if* implemented using the
equivalence, which is certainly reasonable though not required, it
will set errno (and not do any "bad" UB). That is, portable code
can't rely on it setting errno *or* rely on it not doing so.
- David.Thompson1 at worldnet.att.net This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Joe |
last post by:
I'm using Python 2.4 on Windows XP SP2.
I'm trying to receive a command line argument that is a newline (\n)
Here is the command line to use
sample.py "\n"
Here is a sample.py script
|
by: mark lawler |
last post by:
Ok, for my assignment all my classes are working now *hooray* and all I need
to do is set up
the reading from the command line so the user can input commands. Im using
getline and a stringstream...
|
by: Bernd Danberg |
last post by:
Hi,
I have a real strange problem with the command line arguments given to the
main-function and together with using std::string:
#include <string>
int _tmain(int argc, _TCHAR* argv)
{...
|
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...
|
by: Roman Mashak |
last post by:
Hello, All!
I'm implementing simple CLI (flat model, no tree-style menu etc.). Command
line looks like this: <command> <param1> <param2> ... <paramN> (where
N=1..4)
And idea is pretty simple:
...
|
by: News |
last post by:
Hi Everyone,
The attached code creates client connections to websphere queue managers
and then processes an inquiry against them.
The program functions when it gets options from the command...
|
by: Andrew Robert |
last post by:
Hi Everyone.
I tried the following to get input into optionparser from either a file
or command line.
The code below detects the passed file argument and prints the file
contents but the...
|
by: =?Utf-8?B?UmFtb24gR2VuZQ==?= |
last post by:
Hello:
I am building a console application and I am having an issue with the
command line arguments. A couple of my arguments are paths and they are
usually enclosed in double quotes. If one of...
|
by: lilyumestar |
last post by:
I have project I have to do for class. We have to write 4 different .java files.
Project2.java
HouseGUI.java
House.java
HouseSorting.java
I already finish House.java and I need to work on...
|
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...
|
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...
|
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...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |