首页 > 解决方案 > 如何处理-?在命令行中使用 C?

问题描述

我必须处理命令行输入,如果参数之一等于“-?”,我必须写下我在这里所做的解释是我的代码的样子:

#include <stdio.h>
#include <string.h>
#include "header.h"
#define ul unsigned long


int main(int argc, char *argv[]){
    for(int i = 0; i < argc; i++){
        printf("%s\n", argv[i]);
    }
    if(argc > 1){
        if(strcmp(argv[1], "-?") == 0){
            printf("Here I compare 2 strings using my prototype of strcmp(). You have to pass .\\a.out and 2 strings you want to compare.\nIf you want to see their length, type -v after passing values of strings:)\n");
        }
    }
    if(argc > 3){
        if(strcmp(argv[3], "-v") == 0){
            printf("The length of the 1st string: %lu\nThe length of the 2nd string: %lu\n", strlen(argv[1]), strlen(argv[2]));
        }
    }
    char *s1 = argv[1];
    char *s2 = argv[2];
    printf("%i\n", strcmp1(s1, s2));
}

如果我通过了,这就是我得到的./a.out -?

zsh: no matches found: -?

所以,我的问题是我如何处理“-?”?

标签: ccommand-linecommand-line-arguments

解决方案


推荐阅读