在命令行参数中读取 [英] reading in command line argument

查看:77
本文介绍了在命令行参数中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图从命令行参数中读取几个东西,并且

其中一个假设是一个整数,但是当我尝试将它转换为从一个

char *到一个整数,它给出了奇怪的数字......任何想法应该怎么做

i呢....


这就是我这样做的方式


void main(int argc,char * argv [])


{


....


int n;


n =(int)argv [1 ];


....


}

-

已发布通过 http://dbforums.com

推荐答案

nic977< me ********* @ dbforums.com>潦草地写道:
nic977 <me*********@dbforums.com> scribbled the following:
我试图从命令行参数中读取几个东西,并且
其中一个假设是一个整数,但当我尝试从< br *> char *为一个整数,它给出了奇怪的数字......任何想法我应该怎么做....


你们是不应该把它强制转换为整数。查找strtol()。

这就是我这样做的方式



void main(int argc,char * argv [])


未定义的行为。这应该是:

int main(int argc,char * argv [])

{
....
int n;
n =(int)argv [1];


正如我所说,这完全是错误的。 strtol()是你的朋友。

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

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

我正在寻找自己。你有没见过我?

- Anon



--
/-- 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>在消息中写道

news:bj ********** @ oravannahka.helsinki.fi ...

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bj**********@oravannahka.helsinki.fi...
nic977< me ***** ****@dbforums.com>潦草地写道:
nic977 <me*********@dbforums.com> scribbled the following:
我试图从命令行参数中读取几个东西,并且
其中一个假设是一个整数,但当我尝试从< br *> char *为一个整数,它给出了奇怪的数字......任何想法我应该怎么做....
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....



你是不应该把它强制转换为整数。查找strtol()。



You''re not supposed to cast it to an integer. Look up strtol().




我总是使用atol。他们之间有什么区别吗?


-

杰夫

-je6543来自yahoo.com



I always use atol. Is there any difference between them ?

--
Jeff
-je6543 at yahoo.com


在文章< bj ********* @ imsp212.netvigator.com>中,Jeff写道:
In article <bj*********@imsp212.netvigator.com>, Jeff wrote:

Joona I Palaste" < PA ***** @ cc.helsinki.fi>在消息中写道
新闻:bj ********** @ oravannahka.helsinki.fi ...

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bj**********@oravannahka.helsinki.fi...
nic977< me ********* @ dbforums.com>潦草地写下:
nic977 <me*********@dbforums.com> scribbled the following:
>我试图从命令行参数中读取几个东西,并且
>其中一个假设是一个整数,但当我尝试从一个
> char *为一个整数,它给出了奇怪的数字......任何想法应该如何
>我这样做....
> 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....



你不应该把它变成一个整数。查找strtol()。



You''re not supposed to cast it to an integer. Look up strtol().



我总是使用atol。它们之间有什么区别吗?



I always use atol. Is there any difference between them ?




atol(const char * nptr)


相当于


strtol(nptr,(char **)NULL,10)


....但atol不会以任何方式影响errno。同样对于atol,如果

结果中的值无法表示,则行为是

undefined。

-

Andreas K?h?ri



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


这篇关于在命令行参数中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆