扫描并确定在命令提示符中输入的参数! (很难)

时间:2012-12-07 03:06:33

标签: c windows command-line-arguments

快速编辑:这是一项家庭作业。我的目标是为我的程序(-s,-w,宽度长度和文件)接受一些cl参数,并自动换行根据默认长度为40个字符的文件或者如果用户选择的新数字'-w'选项。

我正在尝试编写一个C程序,它通过命令提示符接收参数(可执行文件名为“wrapfile.exe”)。程序没有完成,还有更多要添加,这只是导致我混乱的一部分。

以下是有效命令提示条目的示例:

C:\"within wrapfile.exe's directory"> wrapfile -s filename.txt
C:\"within wrapfile.exe's directory"> wrapfile -w 5 filename.txt
C:\"within wrapfile.exe's directory"> wrapfile -s -w 50 filename.txt

等。

无效条目示例:

C:\"within wrapfile.exe's directory"> wrapfile 
C:\"within wrapfile.exe's directory"> wrapfile -w
C:\"within wrapfile.exe's directory"> wrapfile qwer

我的问题是我输入“-w”后无法检测到数字。

以下是代码:

#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "string.h"


int main(int argc, char *argv[])
{
int output = 0;
int commands = 1;
int wraplength= 41;
int i=0;
int counter=0;
int wordwrap = 0;
int ExitStatus = 1;
int input = 1;
int w = 0;
int s = 0;
FILE *f = NULL;


for (i=0; i < argc; i++)
{
    if ( (*argv[input] + i-1) == '-') // check for option
    {
        printf(" - detected first");
        if (*(argv[input] + i  ) == 's') // check for wordwrap
        {
            printf(" s detected");
            i++;
            i++;
            s = 1; // set s to true to that option can be added later
            wordwrap = 1; // set wordwrap on or true
        }   

        if (*(argv[input] + i) ==  'w')//if the second option is a w
        {
            i++;
            printf(" w detected ");
            sscanf ((argv[input] + i), "%d", &wraplength);
            printf ("%d", wraplength);
            if ( wraplength < 1) // check what the number is
                {
                    printf("Usage: wrapfile [-s] [-w width] file ...");
                    return 2; // command line options incorrect
                }               
        }

        if (*(argv[input] + i) == '-')
        {
            printf(" second - detected");
            i++;

            if (*(argv[input]+ i) ==  'w')//if the second option is a w
            {
                i++;

                if (sscanf ((argv[(input)+1]), "%d", &wraplength) != 1) // check what the number is
                {
                    printf("Usage: wrapfile [-s] [-w width] file ...");
                    return 2; // command line options incorrect
                }
            }
        }
    }
}
return 0;
}

大编辑: 我接受了Dietrich Epp的建议,这是我用它做的事情。每当我尝试在“-s”之后检查一个参数时,我的程序似乎崩溃了。如何在不崩溃程序的情况下检查下一个参数(如果没有?)。我知道这条线与崩溃有关:

   arg = argv[i++];

以下是代码:

while (i < argc) 
{
    arg = argv[i++];
    if (!strcmp(arg, "-s")) 
    {
        arg = argv[i++];
        son = 1; 
        printf("Have -s\n");
        if (!strcmp(arg, "-w"))
        {
            if (i >= argc)
            {
                printf("Usage: wrapfile [-s] [-w width] file ...");
            }
            param = argv[i++];
            wraplength = *param;
            printf("Have -w %s\n", param);
        }

    }

1 个答案:

答案 0 :(得分:1)

我认为你在这里混淆了你的循环变量。

这会使i遍历所有参数,包括 argv[0],这些参数通常是您不想要的。

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

这使用i作为其中一个参数字符串的索引,但语法很有趣。

if (*(argv[input] + i  ) == 's')

在其他系统上,您只需使用getopt(),但在Windows上这并不总是一个选项。

建议

你需要一个更像这样的循环:

// Note: C99, so you will need to translate to C89 if you use Microsoft's
// C compiler
int i = 1;
while (i < argc) {
    char *arg = argv[i++];
    if (!strcmp(arg, "-s")) {
        printf("Have -s\n");
    } else if (!strcmp(arg, "-w")) {
        if (i >= argc)
            error();
        char *param = argv[i++];
        printf("Have -w %s\n", param);
    } else {
        error();
    }
}

命令选项解析非常令人难以置信 与您的程序性能相关,上述if / else块和strcmp()链条都可以。

警告!

能够用这个来指定任意文件名!如果您从main()获得参数,它们将被转换为您当前使用的任何代码页,这几乎可以用于任何目的。 (如果你是唯一一个使用该程序的人,可能可以。)

为了指定任意文件名,您需要调用GetCommandLineW()以UTF-16获取命令行,然后CommandLineToArgvW()将其解析为int argc和{{1 }}

相关问题