发布于 2024-12-11 11:40:36 字数 1607 浏览 0 评论 0原文

我试图创建一个程序,在其中我可以将用户输入的命令和参数解析为特定的数组(这些命令和参数将执行诸如“ls”,“ls -l”,“ls -l | wc”之类的命令但是,我在解析时遇到问题:

        //Split the command and store each string in parameter[]
    cp = (strtok(command, hash));                      //Get the initial string (the command)
    parameter[0] = (char*) malloc(strlen(cp)+ 1);                     //Allocate some space to the first element in the array
    strncpy(parameter[0], cp, strlen(cp)+ 1);
    for(i = 1; i < MAX_ARG; i++)
    {
    cp = strtok(NULL, hash);                 //Check for each string in the array
    parameter[i] = (char*) malloc(strlen(cp)+ 1);
    strncpy(parameter[i], cp, strlen(cp)+ 1);                      //Store the result string in an indexed off array
        if(parameter[i]  == NULL)
        {
            break;
        }
    if(strcmp(parameter[i], "|") == 0)
    {
        cp = strtok(NULL, hash);
        parameter2[0] = (char*) malloc(strlen(cp)+ 1);
        strncpy(parameter2[0], cp, strlen(cp)+ 1);
        //Find the second set of commands and parameters
        for (j = 1; j < MAX_ARG; j++)
        {
            cp = strtok(NULL, hash);
            if (cp == NULL)
            {
                leave = 1;
                break;
            }
            else
            {
                parameter2[j] = (char*) malloc(strlen(cp)+ 1);
                strncpy(parameter2[j], cp, strlen(cp)+ 1);
            }

        }
    }
    if (leave == 1)
    {
        break;
    }
}

当我执行 if (strlen(cp) == NULL) 时遇到问题,存在分段错误。 一旦所有输入都输入到数组中,我就试图打破更大的 for 循环。我可以成功地将正确的字符串元素输入到数组中,但一旦这样做我就无法退出循环。

I was trying to create a program where I could parse the command and parameters enters by the user into specific arrays (these commands and params would execute commands like "ls", "ls -l", "ls -l | wc" However, I am having problem with parsing:

        //Split the command and store each string in parameter[]
    cp = (strtok(command, hash));                      //Get the initial string (the command)
    parameter[0] = (char*) malloc(strlen(cp)+ 1);                     //Allocate some space to the first element in the array
    strncpy(parameter[0], cp, strlen(cp)+ 1);
    for(i = 1; i < MAX_ARG; i++)
    {
    cp = strtok(NULL, hash);                 //Check for each string in the array
    parameter[i] = (char*) malloc(strlen(cp)+ 1);
    strncpy(parameter[i], cp, strlen(cp)+ 1);                      //Store the result string in an indexed off array
        if(parameter[i]  == NULL)
        {
            break;
        }
    if(strcmp(parameter[i], "|") == 0)
    {
        cp = strtok(NULL, hash);
        parameter2[0] = (char*) malloc(strlen(cp)+ 1);
        strncpy(parameter2[0], cp, strlen(cp)+ 1);
        //Find the second set of commands and parameters
        for (j = 1; j < MAX_ARG; j++)
        {
            cp = strtok(NULL, hash);
            if (cp == NULL)
            {
                leave = 1;
                break;
            }
            else
            {
                parameter2[j] = (char*) malloc(strlen(cp)+ 1);
                strncpy(parameter2[j], cp, strlen(cp)+ 1);
            }

        }
    }
    if (leave == 1)
    {
        break;
    }
}

I run into a problem when I do if (strlen(cp) == NULL), there is a segmentation fault.
I am trying to break out of the larger for loop once all the inputs have been entered into arrays. I can succesfully enter the correct string elements into the arrays but I just can't exit the loop once I have done so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2024-12-18 11:40:36

如果找不到更多标记,strtok 可能会返回 NULL 指针。因此,在使用之前必须检查 cp 值:

cp = strtok(NULL, hash);
if (cp != NULL)
{
    ........
}

strtokmay return NULL pointers if can't found more tokens. So, you must check cp value before using it:

cp = strtok(NULL, hash);
if (cp != NULL)
{
    ........
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文