在命令行参数中读取

编程入门 行业动态 更新时间:2024-10-10 04:23:47
本文介绍了在命令行参数中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图从命令行参数中读取几个东西,并且 其中一个假设是一个整数,但是当我尝试将它转换为从一个 char *到一个整数,它给出了奇怪的数字......任何想法应该怎么做 i呢.... 这就是我这样做的方式 void main(int argc,char * argv []) { .... int n; n =(int)argv [1 ]; .... } - 已发布通过 dbforums

推荐答案

nic977< me ********* @ dbforums>潦草地写道: nic977 <me*********@dbforums> scribbled the following: 我试图从命令行参数中读取几个东西,并且其中一个假设是一个整数,但当我尝试从 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 +++ | | 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+++| | 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>潦草地写道: nic977 <me*********@dbforums> 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。他们之间有什么区别吗? - 杰夫 -je6543来自yahoo

I always use atol. Is there any difference between them ? -- Jeff -je6543 at yahoo

在文章< bj ********* @ imsp212vigator>中,Jeff写道: In article <bj*********@imsp212vigator>, 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>潦草地写下: nic977 <me*********@dbforums> 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

更多推荐

在命令行参数中读取

本文发布于:2023-05-25 19:52:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/236196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令行   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!