簡體   English   中英

strtok_s 忽略第一個字符

[英]strtok_s ignors first characters

我正在嘗試獲取輸入並將其傳遞給四個變量名。

我在每個strtok_s之后進行檢查,看看我得到了什么,但第一個單詞只在 4 個字符之后才算。

我的代碼:

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;
}

strtok_s的最后一個參數不正確,它應該是一個指針, strtok_s使用它來存儲其內部 state。

您的 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
}

另一個問題(盡管不是主要問題)是 function 具有void返回類型並返回int

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM