【问题标题】:Counting the amount of string tokens in C计算 C 中字符串标记的数量
【发布时间】:2014-11-17 21:52:06
【问题描述】:

我需要制作一个模拟 Linux 终端的程序。由于某些系统调用需要 1,2 个或更多参数,因此我想确保给定的参数数量是正确的。我使用strtok() 将调用名称与参数分开,但我需要知道strtok() 创建了多少个标记来比较它。

下面是示例代码:

char *comand = (char*) malloc(sizeof(char)*100);
char *token;
char *path1 = (char*) malloc(sizeof(char)*100);
char *path2= (char*) malloc(sizeof(char)*100);



    fgets(comand, 100, stdin);

    printf( "\nYou entered: %s \n", comand);

    token = strtok(comand ," ");

    //Check the number of tokens and add a condition in each IF to match

    if (strcmp("ls",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);

    }
    else if (strcmp("cat",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);

    }
    else if (strcmp("cp",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);
        token = strtok(NULL," ");
        strcpy(path2,token);

    }
    else if (strcmp("mv",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);
        token = strtok(NULL," ");
        strcpy(path2,token);    

    }
    else if (strcmp("find",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);

    }
    else if (strcmp("rm",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);

    }
    else if (strcmp("mkdir",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);
    }
    else if (strcmp("rmdir",token) == 0) {
        token = strtok(NULL," ");
        strcpy(path1,token);
    }
    else if (strcmp("quit",token) == 0) {
        exit(0);
    }
    else print("Number of parameters do not match);

【问题讨论】:

  • “系统调用”?从控制台?
  • 不,这个“终端”的“系统调用”是用 C 语言编写的程序,而不是 Unix 程序。如果命令是“ls”,那么我会调用一个名为“commLs”或类似名称的过程,并将令牌传递给它。
  • 您可能还想检查这个问题:stackoverflow.com/questions/13078926/…
  • 引号怎么办?

标签: c parameters terminal token strtok


【解决方案1】:

strtok() 所做的唯一事情就是查找下一次出现的分隔符并用\0 覆盖该字符并返回添加了偏移量的指针。该指针保存在一个静态变量中,这就是为什么随后使用 char * 的 NULL 对其进行调用将在从找到最后一个分隔符的偏移量处使用的最后一个字符串上执行它。

这个页面有一个很好的例子: http://en.cppreference.com/w/c/string/byte/strtok

如果您只想计算参数,使用strchr() 会更容易,此函数搜索字符并返回指向其位置的指针。你可以像这样使用它。

unsigned int i = 0;
char *temp = token;
while ( (temp = strchr(temp, '') != NULL) ) {
    ++i;
}

这有一个额外的好处,即不会修改您的原始 char 数组,而 strtok() 会这样做!

我会在您为每个命令创建的函数中处理这个问题。

您将所有选项传递给函数并在那里解析它。使用strtok() 或您想使用的任何其他内容。

这在子例程中保持整洁,您将始终知道会发生什么。

if (strcmp("ls",token) == 0) {
    token = strtok(NULL," ");
    strcpy(path1,token);      // I would maybe change the path variable name to args
    ret = lscmd(path1);
    if (ret == -1) {
         // invalid input detected
    }  
}

那么你就会有一个 ls 函数

int lsdcmd(char *args) {
    // parse args for argumants you are looking for, return -1 if it fails

    // do whatever you want to do.
}

【讨论】:

    【解决方案2】:

    您可以通过这种方式使用 strtok 计算参数:

    例子:

    const char* delimiter = ",";
    char* tokens[MAX_NUM_OF_ARGS];
    
    unsigned int index = 0;
    char* temp = strtok(buff,delimiter);
    while (temp!=NULL){
        if(index<MAX_NUM_OF_ARGS){
            tokens[index]=temp;
        }
        index++;
        temp = strtok(NULL,delimiter);
    }
    

    然后你可以遍历指针(令牌)数组并比较它们......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多