参数处理 [英] Argument Processing

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

问题描述



您好,


我正在尝试学习如何在C中处理命令行参数。

下面我编写的代码段作为测试程序进行编译,但是当我尝试运行它时,我会尝试运行它,它是核心转储。这是在FreeBSD环境下。

我在这里做错了什么?


/ *


只是echos屏幕上的命令行参数

测试参数处理的格式


* /


#include< ; stdio.h>

#include< string.h>

int main(int argc,char argv [])

{

int i; / *通用计数器* /


printf(" argc =%d",argc);

for(i = 0; i< = argc ; i ++)

{

printf(" argv [%d] =%s \ n",i,argv [i]);

}


/ *返回操作系统* /

返回(0);

}

-

Daniel Rudy


电子邮件地址已被编码以减少垃圾邮件。

删除所有号码,然后删除无效,电子邮件,否和垃圾邮件以进行回复。


Hello,

I''m trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}
--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.

推荐答案

2004年11月5日星期五05:41:31 GMT,Daniel Rudy

< i0 ********************************** @ n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6>

在comp.lang.c中写道:
On Fri, 05 Nov 2004 05:41:31 GMT, Daniel Rudy
<i0**********************************@n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6>
wrote in comp.lang.c:

你好,

我正在努力学习如何命令行参数在C中处理。我作为测试程序编写的以下代码段编译,但是当我尝试运行它时,它会进行核心转储。这是在FreeBSD环境下。
我在这里做错了什么?

/ *

只需将命令行参数回显到屏幕上
测试参数处理的格式

* /
#include< stdio.h>
#include< string.h>

int main(int argc,char argv [])
{
int i; / *通用计数器* /

printf(" argc =%d",argc);
for(i = 0; i< = argc; i ++)


替换< ="用<在循环。 argv [argv]是一个空指针。

{
printf(" argv [%d] =%s \ n",i,argv [i]);
}

/ *返回操作系统* /
返回(0);


注意''return''是一个声明,而不是一个函数。括号不会造成伤害,但自从第一个ANSI标准15年以来没有被要求。

}

Hello,

I''m trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
Replace "<=" with "<" in the loop. argv[argv] is a null pointer.
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
Note that ''return'' is a statement, not a function. Parentheses do no
harm, but have not been required since the first ANSI standard 15
years ago.
}




-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top。 html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~ a ... FAQ-acllc.html



--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html




" Daniel Rudy"

< ; i0 ********************************** @ n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6>

WRO消息新闻:fo ***************** @ newssvr14.news.prodigy.co m ...

"Daniel Rudy"
<i0**********************************@n0o1p2a3c4b5 e6l7l8s9p0a1m2.3n4e5t6>
wrote in message news:fo*****************@newssvr14.news.prodigy.co m...

你好,

我正在尝试学习如何在C中处理命令行参数。
我作为测试程序编写的代码段编译,但是当我尝试运行时它,核心转储。这是在FreeBSD环境下。
我在这里做错了什么?

/ *

只需将命令行参数回显到屏幕上
测试参数处理的格式

* /
#include< stdio.h>
#include< string.h>

int main(int argc,char argv [])
{
int i; / *通用计数器* /

printf(" argc =%d",argc);
for(i = 0; i< = argc; i ++)
{
printf(" argv [%d] =%s \ n",i,argv [i]);
}

/ *返回操作系统* /
返回(0);
}

您从命令行传递给main的是什么?

您可以尝试在第一个printf之前添加它。


if(argc< 3)

{

printf(" main\\\
的参数不足);

返回0;

}

- Ravi


-
Daniel Rudy

电子邮件地址已被编码以减少垃圾邮件。
删除所有号码,然后删除无效,电子邮件,否和垃圾邮件以进行回复。

Hello,

I''m trying to learn how command line arguments are handled in C. The
following code segment that I wrote as a test program compiles, but when
I try to run it, it core dumps. This is under a FreeBSD environment.
What am I doing wrong here?

/*

just echos the command line arguments onto the screen
tests the format of argument processing

*/

#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}
What are you passing to main from command line ?
You might try adding this before the first printf.

if ( argc < 3 )
{
printf ("Insufficient arguments to main\n");
return 0;
}
- Ravi


--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.



Daniel Rudy写道:
Daniel Rudy wrote:
#include< stdio.h>
#include< string.h>

int main( int argc,char argv [])
{
int i; / *通用计数器* /

printf(" argc =%d",argc);
for(i = 0; i< = argc; i ++)


将此重写为

for(i = 0; i< argc; i ++)

{
printf(" argv [ %d] =%s \ n",i,argv [i]);
}

/ *返回操作系统* /
return(0);
}
#include <stdio.h>
#include <string.h>
int main(int argc, char argv[])
{
int i; /* generic counter */

printf("argc = %d", argc);
for (i = 0; i <= argc; i++)
Rewrite this as
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}

/* return to operating system */
return(0);
}




Satyajit

---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== ----
http://www.newsfeeds.com #1世界新闻组服务! > 100,000新闻组

--- =东/西海岸服务器场 - 通过加密的总隐私= ---



Satyajit
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---


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

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