函数getopt

tolixiaohui 2006-09-20 10:38:27
while ((c = getopt(argc, argv, "R:X:o:1234ABLMPUW5678abcflmprsuvwz?")) != -1)
getopt是不是unix下的函数啊?有没有人能写个windows版本的?
...全文
503 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
windyloft 2006-09-21
  • 打赏
  • 举报
回复
下面是msdn上的例子,参考一下

///////////////////////////////////////////////////////////////////////////////
//
// FILE: getopt.h
//
// Header for the GetOption function
//
// COMMENTS:
//
///////////////////////////////////////////////////////////////////////////////

// function prototypes
int GetOption (int argc, char** argv, char* pszValidOpts, char** ppszParam);


///////////////////////////////////////////////////////////////////////////////
//
// FILE: getopt.c
//
// GetOption function
//
// FUNCTIONS:
//
// GetOption() - Get next command line option and parameter
//
// COMMENTS:
//
///////////////////////////////////////////////////////////////////////////////

#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include "getopt.h"

///////////////////////////////////////////////////////////////////////////////
//
// FUNCTION: GetOption()
//
// Get next command line option and parameter
//
// PARAMETERS:
//
// argc - count of command line arguments
// argv - array of command line argument strings
// pszValidOpts - string of valid, case-sensitive option characters,
// a colon ':' following a given character means that
// option can take a parameter
// ppszParam - pointer to a pointer to a string for output
//
// RETURNS:
//
// If valid option is found, the character value of that option
// is returned, and *ppszParam points to the parameter if given,
// or is NULL if no param
// If standalone parameter (with no option) is found, 1 is returned,
// and *ppszParam points to the standalone parameter
// If option is found, but it is not in the list of valid options,
// -1 is returned, and *ppszParam points to the invalid argument
// When end of argument list is reached, 0 is returned, and
// *ppszParam is NULL
//
// COMMENTS:
//
///////////////////////////////////////////////////////////////////////////////

int GetOption (
int argc,
char** argv,
char* pszValidOpts,
char** ppszParam)
{
static int iArg = 1;
char chOpt;
char* psz = NULL;
char* pszParam = NULL;

if (iArg < argc)
{
psz = &(argv[iArg][0]);
if (*psz == '-' || *psz == '/')
{
// we have an option specifier
chOpt = argv[iArg][1];
if (isalnum(chOpt) || ispunct(chOpt))
{
// we have an option character
psz = strchr(pszValidOpts, chOpt);
if (psz != NULL)
{
// option is valid, we want to return chOpt
if (psz[1] == ':')
{
// option can have a parameter
psz = &(argv[iArg][2]);
if (*psz == '\0')
{
// must look at next argv for param
if (iArg+1 < argc)
{
psz = &(argv[iArg+1][0]);
if (*psz == '-' || *psz == '/')
{
// next argv is a new option, so param
// not given for current option
}
else
{
// next argv is the param
iArg++;
pszParam = psz;
}
}
else
{
// reached end of args looking for param
}

}
else
{
// param is attached to option
pszParam = psz;
}
}
else
{
// option is alone, has no parameter
}
}
else
{
// option specified is not in list of valid options
chOpt = -1;
pszParam = &(argv[iArg][0]);
}
}
else
{
// though option specifier was given, option character
// is not alpha or was was not specified
chOpt = -1;
pszParam = &(argv[iArg][0]);
}
}
else
{
// standalone arg given with no option specifier
chOpt = 1;
pszParam = &(argv[iArg][0]);
}
}
else
{
// end of argument list
chOpt = 0;
}

iArg++;
*ppszParam = pszParam;
return (chOpt);
}
viyar 2006-09-21
  • 打赏
  • 举报
回复
windows also can use this routine

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧