Get the command parameters in Win app and Console app

本文介绍了一种在Windows和控制台应用程序中解析命令行参数的方法。通过示例代码展示了如何逐个提取并处理命令行参数,适用于多种应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Usually one exe command is followed by some parameters, such as c:/dir /a; our application can process those parameters, both in win app and console app.

Indicate that all paremeters are Separated by space ' ';

 

// win mode:

void ParseCommandLineArgs()

{

    CString strCmdLine = GetCommandLine();

 

    if (!strCmdLine.IsEmpty())

    {

       // unify the letters

       strCmdLine.MakeUpper();

 

       int nPos = 0;

       do {

           nPos = strCmdLine.Find(TCHAR(' '));

           if (nPos>0)

           {

              // remove the first parameter, it's usually the exe file path

              strCmdLine.Delete( 0, nPos+1);

              CString strCurrent = strCmdLine;

              int nNextPos = strCmdLine.Find(TCHAR(' '));

              // store the current parameter in strCurrent

              if (nNextPos > 0)

                  strCurrent = strCmdLine.Left( nNextPos );

                  // process the parameter

           }

       } while( nPos != -1);

    }

}

 

console mode: (from open SSL Evp_text.c)

Indicate that all paremeters are Separated by space ':';

Its also an easy script debugger.

// the delim is a stop character.

// return the first part,

// and terminate the string.

static char *sstrsep(char **string, const char *delim)

{

    char isdelim[256]; // stop character table

    char *token = *string;

 

    if (**string == 0)

       return NULL;

 

    memset(isdelim, 0, 256);

    isdelim[0] = 1;   // /0 is a end of string

 

    // initial stop character table

    while (*delim)

    {

       isdelim[(unsigned char)(*delim)] = 1;

       delim++;

    }

 

    // begin check

    while (!isdelim[(unsigned char)(**string)])

    {

       (*string)++;

    }

 

    // end the string by /0

    if (**string)

    {

       **string = 0;

       (*string)++;

    }

 

    return token;

}

// remove the warn information

static unsigned char *ustrsep(char **p,const char *sep)

{ return (unsigned char *)sstrsep(p,sep); }

 

int _tmain(int argc, _TCHAR* argv[])

{

    const char *szPara = argv[1];

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

 

       printf("para%d: %s /n",i, argv[i]);

    }

 

// some examples easy explement of test script

//# CBC-AES128.Encrypt and CBC-AES128.Decrypt

//    AES-128-CBC:2B7E151628AED2A6ABF7158809CF4F3C:000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:7649ABAC8119B246CEE98E9B12E9197D

 

    FILE *f=fopen(szPara,"r");

    if(!f)

       return 0;

    // in a dead loop, we read every line of script

    for (;;) {

 

       char line[4096];

       char *p;

       char *cipher;

       unsigned char *iv,*key,*plaintext,*ciphertext;

       int encdec; // operation flag

       int kn,in,pn,cn;

 

       if(!fgets((char *)line,sizeof line,f))

           break;

       if(line[0] == '#' || line[0] == '/n')

           continue;

       p=line;

       cipher=sstrsep(&p,":"); 

       key=ustrsep(&p,":");

       iv=ustrsep(&p,":");

       plaintext=ustrsep(&p,":");

       ciphertext=ustrsep(&p,":");

       if (p[-1] == '/n') {

           p[-1] = '/0';

           encdec = -1;

       } else {

           encdec = atoi(sstrsep(&p,"/n"));

       }

    }

    fclose(f);

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值