下面是一个简单的代码,它接受一个命令行参数,当然不是程序名,并检查它是否是1到32之间的整数。
命令提示符:cd c:\ somefolder
命令提示符:c:\ somefolder>gcc-o myCode myCode.c
命令提示符:c:\ somefolder>myCode 5
命令提示符:c:\ somefolder>您的arg被接受。
命令提示符:c:\ somefolder>myCode^
命令提示:更多?
然后等待另一个输入。我怎么能像检查信件一样检查这个?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if (argc == 1)
    {
        printf("\nPlease enter a cmd-line arg between 1 and 32.\n");
        return 0;
    }

    if (argc == 2)
    {
        int x;
        char *ptr;
        long y = strtol(argv[1], &ptr , 10);
        int l = strlen(argv[1]);

        for (x = 0; x <= (l - 1); x++)
        {

            if (argv[1][x] >= 'a' && argv[1][x] <= 'z')
            {
                printf("\nYou can't enter letters.\n");
                return 0;
            }
            else if (argv[1][x] >= 'A' && argv[1][x] <= 'Z')
            {
                printf("\nYou can't enter letters.\n");
                return 0;
            }
            else
            {
                //printf("\nCeaser shift accepted.\n");
            }
        }

        if (y <= 0 || y > 32)
        {
            printf("\nCeaser shift must be between 1 and 32.\n", y);
            return 0;
        }

    }

    if (argc > 2)
    {
        printf("You entered too many arg.");
        return 0;
    }

    printf("\nYour arg is accepted.\n");

    return 0;
}

最佳答案

这是一个特殊的控制台符号(转义字符[1,2]),你不能做任何事情。
您可以这样运行程序:

myCode "^" # (with double quotes)


myCode ^^

[1]http://ss64.com/nt/syntax-esc.html
[2]http://www.robvanderwoude.com/useless.php#EscapeChar

关于c - Cmd行arg“^”输出“更多?”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37511210/

10-09 13:22