网站建设资讯

NEWS

网站建设资讯

c语言中getopt函数 c语言中get函数的用法

怎么用C语言编写一个功能菜单,使用户可通过选择菜单调用各项功能,

建议楼主学习linux的C语言,,在网上下载一些开源的linux程序,,,里面都有源代码的,可以多看一些源代码。

为澧县等地区用户提供了全套网页设计制作服务,及澧县网站建设行业解决方案。主营业务为网站建设、网站设计、澧县网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

在linux命令行中,,楼主可模仿调用getopt()函数,,

#includestdio.h

#includeunistd.h

#includememory.h

#define PATH_MAX 100

void main(int argc, char **argv)

{

int iret;

char ckey, ch;

char cpath[] = { 0 };

FILE *pfile = NULL;

unsigned long ulcount = 0;

ckey = getopt(argc, argv, "e::d::c::C:r:v::");

switch(ckey)

{ case 'c' :

if (NULL == optarg)

{

printf("Please input your new file's PATH and name\n");

printf("If your path or filename has space,input \\ to connect\n");

gets(cpath);

printf("filename is %s\n", cpath);

pfile = fopen(cpath, "w+");

}

else

{

pfile = fopen(optarg, "w+");

}

if (NULL == pfile)

{

printf("Creat file %s ERROR!\n", cpath);

}

else

{

printf("Creat file %s success!\n", cpath);

}

fclose(pfile);

break;

case 'd':

if (NULL == optarg)

{

printf("\nPlease input PATH and filename you want to del\n");

printf("If your path or filename has space,input \\ to connect\n");

memset(cpath, 0, sizeof(cpath));

gets(cpath);

iret = remove(cpath);

}

else

{

iret = remove(optarg);

}

if (0 != iret)

{

printf("Remove file %s ERROR!\n", cpath);

return;

}

else

{

printf("del file %s success!\n", cpath);

}

break;

case 'e':

if (NULL == optarg)

{

printf("\nPlease input PATH and filename you want to edit\n");

printf("If your path or filename has space,input \\ to connect\n");

memset(cpath, 0, sizeof(cpath));

gets(cpath);

pfile = fopen(cpath, "a+");

}

else

{

pfile = fopen(optarg, "a+");

}

if (NULL == pfile)

{

printf("Creat file %s ERROR!\n", cpath);

return;

}

else

{

printf("Creat file %s success!\n", cpath);

}

printf("Now, input what you want to edit, end edit it with ctrl+d\n", cpath);

while((ch = getchar()) != EOF)

{

fputc(ch, pfile);

ulcount++;

}

printf("Input %ld char in total.\nSave it success.\n", ulcount);

break;

case 'v':

if (NULL == optarg)

{

printf("\nPlease input PATH and filename you want to view\n");

printf("If your path or filename has space,input \\ to connect\n");

default :

break;

}

return;

}

Python笔记:命令行参数解析

有些时候我们需要通过命令行将参数传递给脚本,C语言中有个getopt()方法,python中也有个类似的命令行参数解析方法getopt()。python也提供了比getopt()更简洁的argparse方法。另外,sys模块也可以实现简单的参数解析,本文将对这3种命令行参数解析方法简要介绍。

sys.argv是传入的参数列表,sys.argv[0]是当前python脚本的名称,sys.argv[1]表示第一个参数,以此类推。

命令行运行:

可以看到传入的参数通过sys.argv来获取,它就是一个参数列表。

python的getopt与C语言的的getopt()函数类似。相比于sys模块,支持长参数和短参数,并对参数解析赋值。但它需要结合sys模块进行参数解析,语法格式如下:

短参数为单个英文字母,如果必须赋值需要在后面加英文冒号( : ),长参数一般为字符串(相比短参数,更能说明参数含义),如果必须赋值需要在后面加等号( = )。

命令行运行:

注意:短参数(options)和长参数(long_options)不需要一一对应,可以任意顺序,也可以只有短参数或者只有长参数。

argparse模块提供了很多可以设置的参数,例如参数的默认值,帮助消息,参数的数据类型等。argparse类主要包括ArgumentParser、add_argument和parse_args三个方法。

下面介绍这三个函数的使用方法。

argparse默认提供了 -h | --help 参数:

命令行运行:

下面列出部分参数:

下面来添加参数:

命令行运行:

parse_args() 方法用于解析参数,在前面的示例代码中使用parse_args方法来提取参数值,对于无效或者错误的参数会打印错误信息和帮助信息:

命令行运行:

本文介绍了Python的三种命令行参数解析方法sys.argv、getopt和argparse,可以根据自己的需要进行选择,getopt和argparse两种方法相比来说,建议选择argparse,代码量更少更简洁。更详细的使用方法参考官方文档:

--THE END--

C语言,用getopt的时候 它读进来的包括 ls -a 还有文件名,怎么只读文件名,不要前面的ls -a?

while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)

{

switch (c)

{

case 'c':

commond = optarg;

//printf("My name is XL. %s\n",commond);

break;

case 'f':

fileName = optarg;

printf("His name is ST. %s\n",fileName);

break;

case 's':

这是我以前写的 你参考

高手求教!!!c语言中怎样读取命令行参数?

把main函数声明为int main(int argc,char *argv[])

argc 是参数的个数

如:

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

{

printf("%s",argv[1]);

}

这是一个简单的未做错误判断的echo函数,将上面的源程序编译连接为echo.exe,然后在命令提示符下输入echo hello

这样,argc=2,argv[0]为echo,argv[1]为hello

我没用过linux,不知道上面的回答有没有对上意思。


当前标题:c语言中getopt函数 c语言中get函数的用法
URL分享:http://cdweb.net/article/ddidppc.html