【问题标题】:strtok problems I think我认为的strtok问题
【发布时间】:2013-08-05 01:10:51
【问题描述】:

想不通,有什么帮助吗?我不认为这是 strtok,我很确定这是我的代码。我想不通是怎么想的。 Get 和 Set 导致 sigsevg。如果我在 num = strtof 等之后放置 printf(),则 num 是正确的,但其他命令的解释不正确。

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

typedef struct
{
    float height;
    float width;
    float length;
}Box;

void usage(void)
{

    printf("\nUsage: [command] [parameter] [amount]\n");
    printf("commands:\n\tSet\n\tGet\n");
    printf("parameters:\n\theight\n\twidth\n\tlength\n");
}


int main()
{
    usage();
    Box box1 = {0,0,0};
    int loop = 1;
    float num;
    char cp[65], *delims =  " !@#$%^&*():;/><.,\\?\"";
    char *tok1, *tok2, *tok3, *temp;

beginning:
    while(loop)
    {

        //Read the command from standard input
        char str[65];
        fgets(str, 64, stdin);
        str[64] = 0;

        //Tokenize the string
        strncpy(cp, str, 64);
        tok1 = strtok(cp, delims);
        tok2 = strtok(NULL, delims);
        tok3 = strtok(NULL, delims);

        //Check if tok3 is float
        num = strtof(tok3, &temp);
        if(num != 0)
        {

        }
        else
        {
            usage();
            goto beginning;
        }
        if(tok1 == 'Get' && tok2 == 'height')
        {
            printf("%f", box1.height);
        }
        else if(tok1 == 'Get' && tok2 == 'width')
        {
          printf("%f", box1.width);
        }
        else if(tok1 == 'Get' && tok2 == 'length')
        {
          printf("%f", box1.length);
        }
        else if(tok1 == 'Get')
        {
           usage();
           goto beginning;
        }

        if(tok1 == 'Set' && tok2 == 'height')
        {
          box1.height = num;
          printf("%f", box1.height);
        }
        else if(tok1 == 'Set' && tok2 == 'width')
        {
          box1.width = num;
        }
        else if(tok1 == 'Set' && tok2 == 'length')
        {
          box1.length = num;
        }
        else if(tok1 == 'Set')
       {
         usage();
         goto beginning;
       }

    }
     return 0;
}

【问题讨论】:

  • 标签错误,我认为 strtok 不是问题
  • 如果你不打算使用temp,你应该把NULL传给strtofnum = strtof(tok3, NULL);
  • char cp[65], *delims = " !@#$%^&amp;*():;/&gt;&lt;.,\\?\""; 最好将delims 声明为const char*,例如const char* delims = " !@#$%^&amp;*():;/&gt;&lt;.,\\?\"";

标签: c string strtok


【解决方案1】:
if(tok1 == 'Get' && tok2 == 'height')

C 字符串必须使用双引号,你不能用== 测试它们是否相等,你应该使用strcmp

if(strcmp(tok1, "Get")==0 && strcmp(tok2, "height")==0)

关于strtof

num = strtof(tok3, &temp);

如果您不必使用temp,请使用空指针:

num = strtof(tok3, NULL);

还有一段代码使用goto:

if(num != 0)
{

}
else
{
    usage();
    goto beginning;
}

goto 很难看,请改用continue

if(num == 0)
{
    usage();
    continue;
}

【讨论】:

  • 可以将浮点数与!= 进行比较;您只需要意识到会出现不精确和舍入,并且它们可能会导致不直观的结果。在这种情况下,没有执行任何算术运算,并且零是精确的,所以这里不会发生这个问题。
  • 你们的快速回答令人难以置信。我仍然在 num =strtof(tok3, &temp) 捕获 sigsevg
  • temp 已分配,它是一个char*。注意strtof 的第二个参数是char**
  • 对不起,我不明白。我没有尝试纠正这个
  • @Toxor 与this version 相比,我已尝试将所有建议的更改合并。请注意,我在循环末尾添加了loop = 0;,但这仅适用于在线编译器。
猜你喜欢
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 2011-03-11
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
相关资源
最近更新 更多