Cmd line arg“^”输出“更多?”

时间:2016-05-29 14:41:50

标签: c cmd

以下是一个简单的代码,它接受一个命令行参数,当然不是程序名,而是检查它是否是1到32之间的整数。

  1. cmd提示:cd c:\ somefolder
  2. cmd提示:c:\ somefolder> gcc -o myCode myCode.c
  3. cmd提示:c:\ somefolder> myCode 5
  4. cmd提示:c:\ somefolder>你的arg被接受了。
  5. cmd提示:c:\ somefolder> myCode ^
  6. cmd提示:更多?
  7. 然后它等待另一个输入。我怎样检查这个,就像我检查过字母一样?

    #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 个答案:

答案 0 :(得分:2)

这是特殊的控制台标志(Escape Character [1,2]),你什么也做不了。

你可以这样运行你的程序:

myCode "^" # (with double quotes) 

myCode ^^

[1] http://ss64.com/nt/syntax-esc.html

[2] http://www.robvanderwoude.com/useless.php#EscapeChar