简体   繁体   English

strtok_s 忽略第一个字符

[英]strtok_s ignors first characters

I'm trying to get an input and it to four variable names.我正在尝试获取输入并将其传递给四个变量名。

I'm doing a checkup after every strtok_s to see what I got but the first word only counts after 4 characters.我在每个strtok_s之后进行检查,看看我得到了什么,但第一个单词只在 4 个字符之后才算。

My code:我的代码:

void zeros()
{
    char buffer[81];
    fgets(buffer, sizeof(buffer), stdin);
    printf("%s\n", buffer);
    char *command = strtok_s(buffer, " \t", &buffer);
    printf_s("the command you selcted is %s\n", command);
    char *Matname = strtok_s(NULL, " \t", &buffer);
    printf_s("the name you selcted is %s\n", Matname);
    char *row = strtok_s(NULL, "  \t", &buffer);
    printf_s("the rowsize you selcted is %s\n", row);
    char *col = strtok_s(NULL, " \t", &buffer);
    printf_s("the colsize you selcted is %s\n", col);
    return 0;
}

The last argument of strtok_s is not correct, it should be a pointer which is used by strtok_s to store its internal state. strtok_s的最后一个参数不正确,它应该是一个指针, strtok_s使用它来存储其内部 state。

Your function should look more like this:您的 function 应该看起来更像这样:

void zeros() // or int zeros() if it's supposed to return int
{
    char buffer[81];
    char *ptr;
    fgets(buffer, sizeof buffer, stdin);
    printf("%s\n", buffer);
    char *command = strtok_s(buffer, " \t", &ptr);
    printf_s("the command you selcted is %s\n", command);
    char *Matname = strtok_s(NULL, " \t", &ptr);
    printf_s("the name you selcted is %s\n", Matname);
    char *row = strtok_s(NULL, "  \t", &ptr);
    printf_s("the rowsize you selcted is %s\n", row);
    char *col = strtok_s(NULL, " \t", &ptr);
    printf_s("the colsize you selcted is %s\n", col);
    // return 0; // if function return type is int
}

Another issue, albeit not the main one, is that the function has void return type and returns an int .另一个问题(尽管不是主要问题)是 function 具有void返回类型并返回int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM