检查参数字符串 [英] Checking param string

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

问题描述

代码:

#include< stdio.h>

#include< strings.h>

#include< stdlib .h>


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

{

if(argc == 1)

printf(没有参数!使用--help获取更多信息。);


if(argc == 2& & argv [1] ==" - help")

printf(只是测试,你现在有空。);


if(argv [1] [0] ==" - ")

printf(这不起作用?);


返回0;

}


两个问题:

1)如何将参数与字符串进行比较,也许我应该使用sprintf

后来尝试比较?或者可能有直接的方法吗?

2)我怎样才能得到第一个参数的第一个字符? (理解如何写这个有点难......




在Mac OS X下使用gcc 4版本。

Code:
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

int main (int argc, char const *argv[])
{
if (argc == 1)
printf("No parameters! Use --help to get more information.");

if (argc == 2 && argv[1] == "--help")
printf("Just test, you are free now.");

if (argv[1][0] == "-")
printf("This does not work?");

return 0;
}

Two questions:
1) How can I compare parameters to string, maybe I should use sprintf
and later try comparing? Or maybe there is direct way of doing it?
2) How can I get first char of the first parameter? (It''s kinda hard
to understand how to write this)

Using gcc 4 version under Mac OS X.

推荐答案



" david" < Da ***************** @ gmail.comwrote in message

news:17 ************* ********************* @ 60g2000h sy.googlegroups.com ...

"david" <Da*****************@gmail.comwrote in message
news:17**********************************@60g2000h sy.googlegroups.com...

代码:

#include< stdio.h>

#include< strings.h>

#include< stdlib.h>


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

{

if(argc == 1)

printf(没有参数!使用--help获取更多信息。);


if(argc == 2&& argv [1] ==" - help")

printf(只是测试,你现在有空。);


if(argv [1 ] [0] ==" - ")

printf(这不起作用?;)


返回0;

}


两个问题:

1)如何将参数与字符串进行比较,也许我应该使用sprintf

以后再尝试比较?或者可能有直接的方法吗?

2)我怎样才能得到第一个参数的第一个字符? (理解如何写这个有点难......


Code:
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

int main (int argc, char const *argv[])
{
if (argc == 1)
printf("No parameters! Use --help to get more information.");

if (argc == 2 && argv[1] == "--help")
printf("Just test, you are free now.");

if (argv[1][0] == "-")
printf("This does not work?");

return 0;
}

Two questions:
1) How can I compare parameters to string, maybe I should use sprintf
and later try comparing? Or maybe there is direct way of doing it?
2) How can I get first char of the first parameter? (It''s kinda hard
to understand how to write this)



编译器应该给你第二个和最后一个警告。你是比较一个字符和一个字符串的
。你需要'' - ''创建一个char常量,

strcmp()来比较

if(strcmp(argv [1]," -help")= = 0)

{


}

当你对它进行排序时,你可能希望看到我的选项解析器,在

网站上。

-

免费游戏和编程好吃的东西。
http://www.personal.leeds.ac.uk/~bgy1mm


我理解字符串比较的问题,但是

最后如果,这让gcc给我警告:testas.c:13:警告:

指针和整数之间的比较


我可以访问该参数的某个字符,或者我应该使用一些

字符串命令使这个工作?

argv [1]是一个指向内存中char数组的指针,* argv [1]应该显示

到该数组中的第一个char,与* argv [1] [0]相同。我是对的

(看起来我不是)


大卫


P.S.感谢您的帮助。
I understood the problem with string comparison, but how about the
last if, which makes gcc to give me warning: testas.c:13: warning:
comparison between pointer and integer

Can I access the certain char of that parameter or I should use some
strings commands to make this work?
argv[1] is a pointer to the char array in memory, *argv[1] should show
to the first char in that array, the same as *argv[1][0]. I am right
(It looks that I am not)

david

P.S. Thanks for the help.


david< Da ***************** @ gmail.comwrites:
david <Da*****************@gmail.comwrites:

我理解字符串比较的问题,但是

最后如果,这让gcc给我警告:testas.c:13:警告:

指针和整数之间的比较
I understood the problem with string comparison, but how about the
last if, which makes gcc to give me warning: testas.c:13: warning:
comparison between pointer and integer



代码为:


if(argv [ 1] [0] ==" - ")

printf(这不起作用?);


(你应该引用足以提供上下文)

The code is:

if (argv[1][0] == "-")
printf("This does not work?");

(you should quote enough to give context)


我可以访问该参数的某个字符,还是应该使用一些

字符串命令来使其工作?
Can I access the certain char of that parameter or I should use some
strings commands to make this work?



如果你喜欢arg [1] [0]和

argv [1],你可以逐个获得这些角色错误与 - 相同。这是一个字符串文字,

不是一个字符。使用'' - ''。

You can get at the characters one by one if you like as arg[1][0] and
argv[1][1] etc. The error is with the "-" which is a string literal,
not a character. Use ''-''.


argv [1]是指向内存中char数组的指针,* argv [1]应显示

到该数组中的第一个char,与* argv [1] [0]相同。我是对的

(看起来我不是)
argv[1] is a pointer to the char array in memory, *argv[1] should show
to the first char in that array, the same as *argv[1][0]. I am right
(It looks that I am not)



最后一个不对,但* argv [1]没问题。 * argv [1] [0]正在尝试

将*应用于角色(即argv [1] [0])。


-

Ben。

The last one is not right but *argv[1] is OK. *argv[1][0] is trying
to apply * to character (namely argv[1][0]).

--
Ben.


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

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